亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

CellFormating 事件:更改字體行的行為類似于循環

CellFormating 事件:更改字體行的行為類似于循環

C#
SMILET 2022-11-13 13:37:26
我想在 DGV 中將特定行的字體更改為粗體,其中列(名稱“vu”)中有“false”值。我的代碼有效,但問題是該行的行為就像有循環(出現并快速重復地消失) private void DGV_boiteReception_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)        {                DataGridViewRow row = DGV_boiteReception.Rows[e.RowIndex];                DataGridViewCellStyle style = new DataGridViewCellStyle();                style.Font = new Font(DGV_boiteReception.Font, FontStyle.Bold);                if (row.Cells["vu"].Value.ToString() == "False")                {                    DGV_boiteReception.Rows[e.RowIndex].DefaultCellStyle = style;                }        }
查看完整描述

1 回答

?
偶然的你

TA貢獻1841條經驗 獲得超3個贊

這應該工作得更好:


private void dataGridView1_CellFormatting(object sender,

                                          DataGridViewCellFormattingEventArgs e)

{

    DataGridViewRow row = DGV_boiteReception.Rows[e.RowIndex];


    if (row.Cells["vu"].Value != null )

    {

        e.CellStyle.Font = new Font(DGV_boiteReception.Font,

                row.Cells[0].Value.ToString() == "False" ? 

                                                 FontStyle.Bold : FontStyle.Regular);

    }

}

我只設置了Font,而不是整個Style*(,并且我只按照建議更改了當前格式化單元格的樣式。


我還在測試單元格的值之前檢查是否為 null 并重置了字體樣式。


(*) 出于某種原因,這似乎與您看到的連續重繪有所不同。


如果您的單元格是一個Checkbox單元格,您還應該對這些事件進行編碼:


private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)

{

    dataGridView1.Invalidate();

}


private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)

{

    dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);

}

你不應該那樣True,False只是為了正常CheckBoxes。如果您將復選框設置為允許第三種狀態 ( ThreeState = true),則將是Checked,Unchecked和Indeterminate。


查看完整回答
反對 回復 2022-11-13
  • 1 回答
  • 0 關注
  • 104 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號