שתף באמצעות


Datagridview checkbox column value Problem using VB.NET 2010

Question

Sunday, October 3, 2010 7:52 AM

Hello to everyone,

 I have this problem.i tried many ways.but i couldn't find a solutin for my problem.

I have a datagridview with a checkbox column.i want to check whether checkbox is checked or not.

If the checkbox is selected it should output as "True" in messagebox and if checkbox is not selected it should giving me a message "False".

All replies (8)

Wednesday, October 6, 2010 7:08 AM ✅Answered

Hello, kuldeep09

 

When we click the checkbox, the checkbox value are not committed to the datasource until the CheckBox cell lost its focus, so there may be an inconsistent between the value in the checkbox and the actually value in the datasource when we are editing in the cell.

 

So all we need is to commit the change manually after we click on the check checkbox. See the following code below, the following code should do the trick.

 

    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

        If TypeOf Me.DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex) Is DataGridViewCheckBoxCell Then

            Dim dgvCheckBoxCell As DataGridViewCheckBoxCell = Me.DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex)

            'Commit the data to the datasouce.

            Me.DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)

 

            Dim checked As Boolean = CType(dgvCheckBoxCell.Value, Boolean)

            MessageBox.Show(checked)

        End If

End Sub

 

Hope this helps,

Thanks

Chao

Please unmark it if it does not help, and mark it if it helps.


Sunday, October 3, 2010 8:09 AM

Yea and how did you populate the DataGridView, there are hundreds of ways which influence a good answer on your question, while if I give you direct a cell solution I show only a kind of amateurism.

 

Success
Cor


Sunday, October 3, 2010 9:48 AM

Yea and how did you populate the DataGridView, there are hundreds of ways which influence a good answer on your question, while if I give you direct a cell solution I show only a kind of amateurism.

 

Success
Cor

On datagridview cellcontentclick, under the datagridviewcheckbox column, if i click on the check box then message must be display "Checked", if i check on the box and if not, then message must be "Uncheck"      .


Thursday, November 22, 2012 9:31 AM

Turns out the above solution not working if, the datatable update event is triggered from outside the form i.e. from MDI form !!!

After implementing the above code, I tried to save the data from the "save" button from the MDI form of the application. It failed. But when, I executed the "save" code using a button in the form. It worked perfectly.

In both way, I used the same saving code. Any idea, how to make it work from MDI?

Regards Pranabjyoti (Everyone(not just the thread starter) should take the time to mark helpful posts, propose answers, and mark answers to questions.)


Thursday, November 22, 2012 10:07 AM

Don't hack the thread of somebody else, create your own question. This is very misleading for persons searching for an answer.

Success
Cor


Thursday, November 22, 2012 10:29 AM

I choose to continue this thread because the issue is same. The reply (by Chao Kuo)that was marked as answer, is true for one scenario but not working in another.

I was going crazy why something marked answer is not working for me. And it took me lot of time, to finally discover the reason. It may be the same case for someone else, who atleast won't have to waste as much time as I did.

Anyway, OK am going to open up a new thread on this. You are an experienced guy, pls look into the issue.

Regards Pranabjyoti (Everyone(not just the thread starter) should take the time to mark helpful posts, propose answers, and mark answers to questions.)


Friday, August 30, 2013 5:05 AM | 1 vote

Thanq . . . 


Tuesday, February 25, 2014 3:17 AM

The DataGridView Checkbox does not reflect a user click until losing context on the grid.  This is still a bit simple.  There seems to be more conditions.

Coding without a data source and setting grid values directly, the problem still exists.

The solution for me was to remove statements which restore the grid display position.  Why?  Who knows but it feels like a subtle bug somewhere.