gpt4 book ai didi

c# - DataGridViewCheckboxCell 的背景色

转载 作者:行者123 更新时间:2023-11-30 17:21:13 24 4
gpt4 key购买 nike

我有一个 DataGridView 绑定(bind)到一个对象列表,我正在使用 CellFormatting 事件设置动态单元格背景颜色,如 this answer .这适用于除 DataGridViewCheckboxColumn 之外的每一列。当我单击此单元格内部(但在复选框外部)时,单元格背景更改为默认的白色。

从视觉上看,细胞选择似乎正在发生,尽管我已尽最大努力阻止它。我的单元格格式化代码设置了 SelectionBackColor 以及 BackColor。我已经使用 CellStateChanged 事件禁用了单元格选择,并且其他列均不可选择:

private void PlayerGrid_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e){    if (e.StateChanged == DataGridViewElementStates.Selected)       e.Cell.Selected = false;}

是否有额外的解决方法来覆盖复选框的单元格行为?

最佳答案

我通过将以下代码添加到 CellStateChanged 事件找到了解决方法:

if (e.Cell is DataGridViewCheckBoxCell)
e.Cell.Style.BackColor = BackgroundColor(e.Cell.RowIndex);

(BackgroundColor() 根据行计算单元格背景色。)

这可以解决问题,但可能会导致创建额外的样式对象,从而导致较大表或虚拟表出现性能问题。

关于c# - DataGridViewCheckboxCell 的背景色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3660337/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com