2
votes

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.

4
Try to rephrase your question, it is a bit hard to understand correctly. I do realize English is not your first language.leppie
Hope it makes sense now.IsmailS
is your checkbox bounded to property in ItemsSource ?klm_
I don't want to bind the check box to any data source. Its just that on the click of button I will pick Id's of only those rows (for processing), which has the check box checked.IsmailS
Is DataGridView enabled and NOT readonly? What about it's rows and columns?26071986

4 Answers

3
votes

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

2
votes

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.

1
votes

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");

}
0
votes

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.