Question

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. When the user checked the checkbox, then it should do two things:

  • enable the row of the gridview (the rest of columns, textboxs, combos,...)
  • 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;
                }
            }
        }

    }
Was it helpful?

Solution

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:

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
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top