C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » DataGridView »

Custom Built -DataGridView with Select /Deselect all checkbox


Posted Date: 01 Jul 2009    Resource Type: Code Snippets    Category: DataGridView
Author: Viji RAJKUMARMember Level: Diamond    
Rating: 1 out of 5Points: 10



The following code snippet is used to add CheckBox in Column Header of DataGridView.


Features:


/* When the Column Header check box is selected, all row check boxes will be selected */

/* When the Column Header check box is deselected, all row check boxes will be deselected */

/* When any row check box is deselected while the Column Header check box is selected, the column header check box will be deselected */

/*When the Column Header check box is selected while any of row check box is in edit mode, that checkbox will also get selected.




Dim bForceUnCheck As Boolean = False

:
:
:

Private Sub populateGridView()


'Declaration of variables

Dim strSql As String
Dim dt As New DataTable
Dim cmd As SqlCommand


Me.Cursor = Cursors.WaitCursor

Try
strSql = "SELECT TOP " & nPageSize & " * FROM " & table

cmd = New SqlCommand(strSql, connection)

cmd.CommandTimeout = 999999

dataAdapter = New SqlDataAdapter(cmd)

Me.Cursor = Cursors.WaitCursor

dataAdapter.Fill(dt)

Me.dbGridView.SuspendLayout()


Me.dbGridView.DataSource = dt

Me.dbGridView.ResumeLayout()

Me.Cursor = Cursors.Default


Catch ex As SqlException

MessageBox.Show(ex.Message)

Me.Cursor = Cursors.Default

Me.dbGridView.DataSource = Nothing

Exit Sub

End Try


' Customize DataGridView, add checkbox column

Dim checkboxColumn As DataGridViewCheckBoxColumn = New DataGridViewCheckBoxColumn()

checkboxColumn.Name = "chkBox"

checkboxColumn.Width = 30

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 / 4)

Dim checkboxHeader As CheckBox = New CheckBox()

checkboxHeader.Name = "SELECTALL"

checkboxHeader.Size = New Size(18, 18)

checkboxHeader.Location = rect.Location

AddHandler checkboxHeader.CheckedChanged, AddressOf checkboxHeader_CheckedChanged

DataGridView1.Controls.Add(checkboxHeader)

End Sub






'This Function handles the state change of Checkbox Column Header



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




'This function handles the CheckBox state changed event for each row of datagridview



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 = "chkBox" And e.RowIndex >= 0 Then

Dim dgCB As DataGridViewCheckBoxCell

dgCB = CType(DataGridView1("chkBox", 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





For more details, visit http://vijirajkumar.blogspot.com/2009/07/custom-built-datagridview-with-select.html



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
DataGridView with Select all/Deselect all checkbox  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: DataGridView Checkbox Column Handling in VB.NET
Previous Resource: Displaying various cursors and tool tip text for DataGridView columns
Return to Discussion Resource Index
Post New Resource
Category: DataGridView


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use