CellFormating 事件:更改字体行的行为类似于循环

我想在 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;


                }


        }


SMILET
浏览 157回答 1
1回答

偶然的你

这应该工作得更好: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。
打开App,查看更多内容
随时随地看视频慕课网APP