gpt4 book ai didi

c# - 如何在单击 DatagridView 中的另一个复选框列时禁用复选框列

转载 作者:行者123 更新时间:2023-11-30 17:53:45 25 4
gpt4 key购买 nike

我有两个 ckecboxes(错误和启用),如下所示

enter image description here

如果我取消选中“启用”复选框,相应的“错误”复选框将变为灰色。

enter image description here

我试过如下但没有启用或禁用属性

    void dgRulesMaster_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex > -1 & e.ColumnIndex == 3)
{
var editingCellFormattedValue = Convert.ToBoolean(((DataGridViewCheckBoxCell)dgRulesMaster.Rows[dgRulesMaster.CurrentRow.Index].Cells[3]).EditingCellFormattedValue);

if (editingCellFormattedValue == false)
{
dgRulesMaster[2, e.RowIndex].ReadOnly = true;
}
}
}

最佳答案

同样,使用自定义绘画(我喜欢它:),我想 Cell 在 ReadOnly 时应该看起来像 Disable,当然它只适用于 DataGridViewCheckBoxColumn,这是我的代码:

void dgRulesMaster_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex > -1 & e.ColumnIndex == 3)
{
var editingCellFormattedValue = Convert.ToBoolean(((DataGridViewCheckBoxCell)dgRulesMaster.Rows[dgRulesMaster.CurrentRow.Index].Cells[3]).EditingCellFormattedValue);

if (editingCellFormattedValue == false)
{
dgRulesMaster[2, e.RowIndex].ReadOnly = true;
dgRulesMaster.InvalidateCell(2,e.RowIndex);
}
}
}
//And here is the CellPainting event handler for your dataGridView
private void dgRulesMaster_CellPainting(object sender, DataGridViewCellPaintingEventArgs e){
if (e.ColumnIndex > -1 && e.RowIndex > -1 &&
dgRulesMaster.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn && dgRulesMaster[e.ColumnIndex, e.RowIndex].ReadOnly)
{
Size checkSize = CheckBoxRenderer.GetGlyphSize(e.Graphics, System.Windows.Forms.VisualStyles.CheckBoxState.CheckedNormal);
e.Handled = true;
e.PaintBackground(e.CellBounds, true);
if (e.Value != null)
{
CheckBoxRenderer.DrawCheckBox(e.Graphics, new Point(e.CellBounds.X + e.CellBounds.Width / 2 - checkSize.Width / 2, e.CellBounds.Y + e.CellBounds.Height / 2 - checkSize.Height / 2),
(bool)e.Value ? System.Windows.Forms.VisualStyles.CheckBoxState.CheckedDisabled : System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedDisabled);
}
}
}

关于c# - 如何在单击 DatagridView 中的另一个复选框列时禁用复选框列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16996158/

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