简体   繁体   English

Gridview通过复选框以编程方式更改行BackColor

[英]Gridview change row BackColor programmatically through a checkbox

i am using VS 2010 C# I have a DataGridView not to data source (colums are added manually) and each row has a check box which is should be always enabled. 我正在使用VS 2010 C#,我有一个DataGridView而不是数据源(手动添加列),并且每一行都有一个复选框,应始终启用该复选框。 When the user checked the checkbox, then it should do two things: 当用户选中复选框时,它应该做两件事:

  • enable the row of the gridview (the rest of columns, textboxs, combos,...) 启用gridview的行(其余的列,文本框,组合,...)
  • change the backcolor of the row to white (default is gray, like disabled) 将行的背景色更改为白色(默认为灰色,如禁用)

i tried all provided codes and other solutions but there is no solution for any of the above mentioned requirements. 我尝试了所有提供的代码和其他解决方案,但没有满足上述任何要求的解决方案。 please help .!! 请帮忙 。!! thanks alot 非常感谢

 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;
                }
            }
        }

    }

what have you tried yet ? 您尝试了什么? please provide the code. 请提供代码。 M unable to comment on your question. 我无法评论您的问题。 You can add event to the check boxes and then pick the current row. 您可以将事件添加到复选框,然后选择当前行。 Which will give you the right way to proceed. 这将为您提供正确的进行方法。

Update 2: 更新2:

You Can try THis 你可以试试这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM