2

I've a DataGridView which is working perfectly fine. I use it just to show data.

Now I want ability to select rows by check box and perform an operation for only selected rows on click of a button (this button is out of the grid on the same form). For this purpose, I'm following these steps to add checkbox column to datagridview.

On running the application what is see is I can't check the check box either by mouse click or keyboard. And by its looks I can understand that its not in disabled/readonly state. So whenever I try to click on the checkbox, it changes it's borders normally as an enabled check box does. But finally it is not checking the check box.

Community
  • 1
  • 1
IsmailS
  • 10,797
  • 21
  • 82
  • 134

4 Answers4

3

Try it.

 private void Form1_Load(object sender, EventArgs e)
    {
        DataGridViewCheckBoxColumn ck = new DataGridViewCheckBoxColumn();
        dataGridView1.Columns.Insert(0,ck);
    }

may help you.

Ismail here is your solution of your confusion Dgv-DatabindingCompleteEvent

mahesh
  • 1,370
  • 9
  • 36
  • 61
  • Very strange. It works!!!! Still don't know why it doesn't work when added at design time – IsmailS Oct 18 '10 at 12:38
  • I have a datagrid working with the Checkbox column added in XAML so it's not that this won't work, seems like there is something else going on. – Mark Oct 18 '10 at 13:57
  • @Ismail, Here is above i putted the link which should guide you when should you call this event handler – mahesh Oct 19 '10 at 05:48
2

I went through the same problem. For me the solution was pretty simple. My datagridview had a disabled editing option (because I didn't want the user to change the data) and I wanted to be able to check/uncheck my DataGridViewCheckBoxColumn. So in the dataGridView properties I checked the 'Enable Editing' option, but in the code I've disabled it for every column except of mine desired checkBoxColumn. Hope it will help to somebody.

butterfly
  • 21
  • 1
1

if you want to check the state of all checkBoxes in the dgv:

foreach (DataGridViewRow row in dataGridView1.Rows)
{
    DataGridViewCheckBoxCell chk = row.Cells[0] as DataGridViewCheckBoxCell;

    if (Convert.ToBoolean(chk.Value) == true)
      MessageBox.Show("this cell checked");

}
MyKuLLSKI
  • 5,285
  • 3
  • 20
  • 39
satish
  • 11
  • 1
  • Thanks for the answer @Satish. I know that your answer is informative. But I'm sorry to say that it is far away from what is asked in question. It would be better if you delete this answer or it's surely going to be flagged and removed ultimately. I would recommend that you read [FAQs](http://stackoverflow.com/faq) before posting on this site. – IsmailS May 19 '11 at 07:18
0

In my case I had the gridview loading code in page_load, so after a button click the grid was reloading and lost its selection. So Moving the code inside

if(!isPostback)
{ LoadGrid();}

worked for me.

Put a breakpoint in page_load to understand better.

Kalyani Ramamurthy
  • 378
  • 2
  • 3
  • 13