-
Jul 19th, 2013, 01:26 PM
#1
Thread Starter
PowerPoster
[RESOLVED] Referencing a DataGridViewCheckBox?
I have a DataGridView that has a column defined as a CheckBox. In the CellClick Event I want to be able to check to see if that CheckBox has been checked or not. I'm not sure how to check it. Any ideas?
Thanks,
-
Jul 19th, 2013, 04:04 PM
#2
Re: Referencing a DataGridViewCheckBox?
The CellClick event is an unreliable event for what you're wanting to do, but this is how you'd do it:
Code:
Private Sub DataGridView1_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
If Not (IsNothing(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value)) Then
MessageBox.Show(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString)
End If
End Sub
Here is how I'd do it using the CellValueChanged Event:
Code:
Option Strict On
Option Explicit On
Public Class Form1
Private isloading As Boolean = True
Private Sub DataGridView1_CellValueChanged(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
If Not (isloading) AndAlso Not (IsNothing(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value)) Then
MessageBox.Show(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString)
End If
End Sub
Private Sub DataGridView1_CurrentCellDirtyStateChanged(sender As System.Object, e As System.EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged
If Not (IsNothing(DataGridView1.IsCurrentCellDirty)) AndAlso DataGridView1.IsCurrentCellDirty Then
DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
End If
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'The only way I could think to get past the form_load event.
isloading = False
End Sub
End Class
-
Jul 19th, 2013, 04:10 PM
#3
Re: Referencing a DataGridViewCheckBox?
For a check of the whole column ...
vb.net Code:
For Each row As DataGridViewRow In DataGridView1.Rows
If CBool(CType(row.Cells(0), DataGridViewCheckBoxCell).Value) Then
row.Cells(1).Value = "Yes"
Else
row.Cells(1).Value = "No"
End If
Next
... or if you've specifically designated TrueValue and FalseValue for the column (here as "Yes" and "No"), simply ...
vb.net Code:
For Each row As DataGridViewRow In DataGridView1.Rows
row.Cells(1).Value = row.Cells(0).Value
Next
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Jul 19th, 2013, 04:18 PM
#4
Re: Referencing a DataGridViewCheckBox?
Dun, quick question, and as this pertains to my answer to the OP I hope the OP doesn't feel as though I'm high jacking the thread. Could you see of anyway to get past the form_load event without an InvalidOperationsException being thrown? The only way I could think of was to set up a Boolean flag like I did.
-
Jul 19th, 2013, 07:39 PM
#5
Re: Referencing a DataGridViewCheckBox?
If Me.Visible will do the trick. Just ensures that everything's initialised completely.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Jul 20th, 2013, 01:03 PM
#6
Thread Starter
PowerPoster
Re: Referencing a DataGridViewCheckBox?
dday,
Your solution worked...thanks!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|