|
-
Oct 29th, 2009, 06:21 AM
#1
Thread Starter
Hyperactive Member
DataGridView Select Column
Hi,
I have added an unbound checkbox column to my DGV. I have two questions on handling it:
1. How to have a checkbox on this Column header, which on checking must check all the rows in the DGV and vice versa?
2. How do i get the Boolean value of the checked rows in DGV on event handling? In other words, how do i know what rows are selected?
Please help.
-
Oct 29th, 2009, 07:30 AM
#2
Re: DataGridView Select Column
I don't know about the first question but for the second you answered it yourself. The Value property of each cell will be True or False, depending on whether the box is checked or not. The Value property of every DataGridViewCell always contains the cell's value.
-
Oct 31st, 2009, 04:27 PM
#3
Thread Starter
Hyperactive Member
Re: DataGridView Select Column
Anybody any idea on the first part???
-
Nov 28th, 2009, 06:59 AM
#4
Thread Starter
Hyperactive Member
Re: DataGridView Select Column
I am using the following code to add a select column in my DGV. The header checkbox is enabling me to select/deselect all the displayed rows in DGV but when i try to select an individual row, it doesn't happen.
Code:
Dim bForceUnCheck As Boolean = False
Private Sub MDIParentMW_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim checkboxColumn As DataGridViewCheckBoxColumn = New DataGridViewCheckBoxColumn()
checkboxColumn.Name = "Select"
checkboxColumn.Width = 25
checkboxColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
'Add it is a First Column
DataGridView1.Columns.Insert(0, checkboxColumn)
' Add Checkbox Column Header
Dim rect As Rectangle = DataGridView1.GetCellDisplayRectangle(0, -1, True)
' set checkbox header to center of header cell. +1 pixel to position correctly.
rect.X = rect.Location.X + (rect.Width / 1.55)
Dim checkboxHeader As CheckBox = New CheckBox()
checkboxHeader.Name = "SELECTALL"
checkboxHeader.Size = New Size(14, 20)
checkboxHeader.Location = rect.Location
AddHandler checkboxHeader.CheckedChanged, AddressOf checkboxHeader_CheckedChanged
DataGridView1.Controls.Add(checkboxHeader)
End Sub
Private Sub checkboxHeader_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
If Not bForceUnCheck Then
DataGridView1.EndEdit()
'To override the individually selected cells
For i As Integer = 0 To DataGridView1.RowCount - 1
DataGridView1(0, i).Value = CType(DataGridView1.Controls.Find("SELECTALL", True)(0), CheckBox).Checked
Next
End If
bForceUnCheck = False
End Sub
Private Sub DataGridView1_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
If CType(sender, DataGridView).Columns(e.ColumnIndex).Name = "Select" And e.RowIndex >= 0 Then
Dim dgCB As DataGridViewCheckBoxCell
dgCB = CType(DataGridView1("Select", e.RowIndex), DataGridViewCheckBoxCell)
'Uncheck the header checkbox when any of the check box in the row
'is deselected
If Not CBool(dgCB.EditedFormattedValue) Then
'Checkbox Unselected
If CType(DataGridView1.Controls.Find("SELECTALL", True)(0), CheckBox).Checked Then
bForceUnCheck = True
CType(DataGridView1.Controls.Find("SELECTALL", True)(0), CheckBox).Checked = False
End If
End If
End If
End Sub
Kindly suggest.
Last edited by LuxCoder; Nov 28th, 2009 at 07:02 AM.
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
|