为了账号安全,请及时绑定邮箱和手机立即绑定

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 关注
  • 82 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号