【问题标题】:Gridview change row BackColor programmatically through a checkboxGridview 通过复选框以编程方式更改行 BackColor
【发布时间】:2014-03-31 10:43:33
【问题描述】:

我正在使用 VS 2010 C# 我有一个 DataGridView 不是数据源(列是手动添加的),每一行都有一个应该始终启用的复选框。 当用户选中复选框时,它应该做两件事:

  • 启用 gridview 的行(其余列、文本框、组合...)
  • 将行的背景颜色更改为白色(默认为灰色,如禁用)

我尝试了所有提供的代码和其他解决方案,但没有针对上述任何要求的解决方案。 请帮忙 。!! 非常感谢

 private void dgv1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        if (e.ColumnIndex == 0)
        {
            Boolean Xcheckbox = (Boolean)dgv1.Rows[e.RowIndex].Cells[0].Value;
            if (Xcheckbox == true)
            {
                dgv1.Rows[dgv1.CurrentRow.Index].DefaultCellStyle.BackColor = System.Drawing.Color.Red;                        
            }
            for (int i = 1; i < dgv1.Columns.Count-1; i++)
            {
                {
                    dgv1.Rows[e.RowIndex].Cells[i].ReadOnly = Xcheckbox;
                }
            }
        }

    }

【问题讨论】:

  • 将事件添加到行中的复选框。然后在事件中启用其余的列、文本框和组合并更改颜色。发布您已经尝试过的代码。
  • 我发布了我使用的代码,但仍然无法正常工作,请您帮忙!!非常感谢

标签: c# gridview datagridview colors row


【解决方案1】:

你尝试过什么?请提供代码。 M 无法评论您的问题。您可以将事件添加到复选框,然后选择当前行。这将为您提供正确的方法。

更新 2:

你可以试试这个

private void myDataGrid_OnCellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == myCheckBoxColumn.Index && e.RowIndex != -1)
    {
        // Handle your checkbox state change here
    }
}

【讨论】:

  • 它工作正常,非常感谢! .我想投票给答案,但我仍然没有足够的信用..但无论如何,非常感谢
猜你喜欢
  • 1970-01-01
  • 2016-11-05
  • 1970-01-01
  • 2017-06-18
  • 2015-05-02
  • 1970-01-01
  • 2013-05-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多