IsmailS
IsmailS

Reputation: 10863

Cannot select checkbox in a databounded DataGridView

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.

Upvotes: 2

Views: 9403

Answers (4)

Kalyani Ramamurthy
Kalyani Ramamurthy

Reputation: 398

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.

Upvotes: 0

butterfly
butterfly

Reputation: 21

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.

Upvotes: 2

satish
satish

Reputation: 11

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

}

Upvotes: 1

mahesh
mahesh

Reputation: 1390

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

Upvotes: 3

Related Questions