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.

8
  • Try to rephrase your question, it is a bit hard to understand correctly. I do realize English is not your first language. Commented Oct 18, 2010 at 11:31
  • Hope it makes sense now. Commented Oct 18, 2010 at 11:42
  • is your checkbox bounded to property in ItemsSource ? Commented Oct 18, 2010 at 11:45
  • 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. Commented Oct 18, 2010 at 11:49
  • Is DataGridView enabled and NOT readonly? What about it's rows and columns? Commented Oct 18, 2010 at 12:16

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

3 Comments

Very strange. It works!!!! Still don't know why it doesn't work when added at design time
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.
@Ismail, Here is above i putted the link which should guide you when should you call this event handler
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.

Comments

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

}

1 Comment

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 before posting on this site.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.