Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable and disable particular row in DataGridView by checkbox?

I am trying to enable and disable specific row in DataGridView by checking and unchecking of checkbox inside gridview. (C# Windows application)

I tried using the CellClick event which did not work as expected.

This is the code which i tried

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 0 && dataGridView1.CurrentCell.Selected == true)
    {

        dataGridView1.Columns[3].ReadOnly = false;

    }

}

please tell me how to do this.

Thanks in advance

like image 209
Raj Avatar asked Oct 20 '25 15:10

Raj


1 Answers

I think you missed the CellContentClick event, try this:

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{          
     if (e.ColumnIndex == dataGridView1.Columns["Your Column Name"].Index) //To check that we are in the right column
     {
          dataGridView1.EndEdit();  //Stop editing of cell.
          if ((bool)dataGridView1.Rows[e.RowIndex].Cells["Your Column Name"].Value)
          {
             //dataGridView1.Columns[3].ReadOnly = true;// for entire column 
               int colIndex = e.ColumnIndex;
               int rowIndex = e.RowIndex;
               dataGridView1.Rows[colIndex].Cells[rowIndex].ReadOnly = true;
          }
    }
}
like image 89
iJay Avatar answered Oct 23 '25 06:10

iJay