0

I have a Datagridview in which there is a checkbox column. I want to check a checkbox of particular cell. I have used below code in form load after binding the grid.

But it's not working.

I am binding the grid using for loop.

DataGridViewCheckBoxColumn dgvc = new DataGridViewCheckBoxColumn();
dgvc.Name = "colCheck";
dgvc.Width = 50;               
dgvc.DefaultCellStyle.BackColor = Color.Lavender;                
dgvOption.Columns.Add(dgvc);

WebBrowserColumn wbc = new WebBrowserColumn();
wbc.Name = "colOptionText";
//wbc.Width = 500;
wbc.ReadOnly = true;
wbc.DefaultCellStyle.BackColor = Color.Lavender;         
dgvOption.Columns.Add(wbc);          

dgvOption.Columns.Add("colCorrect", "Correct");
dgvOption.Columns["colCorrect"].Visible = false;

dgvOption.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None;
dgvOption.AllowUserToResizeRows = false;
dgvOption.AllowUserToResizeColumns = false;            
dgvOption.CellBorderStyle = DataGridViewCellBorderStyle.None;
dgvOption.ClearSelection();

int i = 0;    
foreach (DataRow dtRow in dtOption.Rows)
{
    dgvOption.Rows.Add();
    dgvOption.Rows[i].Cells["colOptionText"].Value = dtRow["Options"].ToString();
    dgvOption.Rows[i].Cells["colCorrect"].Value = dtRow["Correct"].ToString();
    i += 1;
}

if(Answer !="")
{
    dgvOption.Rows[2].Cells["colCheck"].Value = true;
}

private void dgvOption_SelectionChanged(object sender, EventArgs e)
{
    dgvOption.ClearSelection();            
}

private void dgvOption_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 0)
    {
        foreach (DataGridViewRow dgvRow in dgvOption.Rows)
        {
            dgvRow.Cells["colCheck"].Value = false;
        }

        dgvOption.CurrentCell.Value = true;
    }
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Raghubar
  • 2,768
  • 1
  • 21
  • 31

2 Answers2

1

You must check the Enable Editing checkbox in the DataGridView Task list. here is the screen shot

enter image description here

Sajitha Rathnayake
  • 1,688
  • 3
  • 26
  • 47
0

Selected property is to select cell, not to check checkbox in it. Use Value property.

dgvOption.Rows[2].Cells["colCheck"].Value = true;
gzaxx
  • 17,312
  • 2
  • 36
  • 54
  • Then there is a problem elsewhere as this is to correct way to check checkbox in datagridview. – gzaxx Jul 23 '13 at 05:56
  • Are there any events like `CellValueChanged` in your code? Also what happens if you add this to your loop `dgvOption.Rows[i].Cells["colCheck"].Value = true;` - are checbox being checked? – gzaxx Jul 23 '13 at 06:03
  • Check i have also posted that events. – Raghubar Jul 23 '13 at 06:07
  • Hmm I don't see anything wrong with that event, have you tried to move checking code into loop and see what happens then? – gzaxx Jul 23 '13 at 06:14
  • Then there is something very wrong with your code. First step would be to put debug when you are checking checkbox and from there go trough code and see if there is `Exception` or some other code unchecking it. This is the only way to check checkbox so for 100% percent there is something wrong with code. – gzaxx Jul 23 '13 at 06:23