Friday, January 29, 2010

datagridviewheadercheckboxcolumn_SELECTALL

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing

Imports System.Text
Imports System.Windows.Forms

Namespace SelectAll
Public Partial Class frmSelectAll
Inherits Form
Private TotalCheckBoxes As Integer = 0
Private TotalCheckedCheckBoxes As Integer = 0
Private HeaderCheckBox As CheckBox = Nothing
Private IsHeaderCheckBoxClicked As Boolean = False

Public Sub New()
InitializeComponent()
End Sub

Private Sub frmSelectAll_Load(ByVal sender As Object, ByVal e As EventArgs)
AddHeaderCheckBox()

AddHandler HeaderCheckBox.KeyUp, AddressOf HeaderCheckBox_KeyUp
AddHandler HeaderCheckBox.MouseClick, AddressOf HeaderCheckBox_MouseClick
AddHandler dgvSelectAll.CellValueChanged, AddressOf dgvSelectAll_CellValueChanged
AddHandler dgvSelectAll.CurrentCellDirtyStateChanged, AddressOf dgvSelectAll_CurrentCellDirtyStateChanged
AddHandler dgvSelectAll.CellPainting, AddressOf dgvSelectAll_CellPainting

BindGridView()
End Sub

Private Sub BindGridView()
dgvSelectAll.DataSource = GetDataSource()

TotalCheckBoxes = dgvSelectAll.RowCount
TotalCheckedCheckBoxes = 0
End Sub

Private Function GetDataSource() As DataTable
Dim dTable As New DataTable()

Dim dRow As DataRow = Nothing
Dim dTime As DateTime
Dim rnd As New Random()

dTable.Columns.Add("IsChecked", System.Type.[GetType]("System.Boolean"))
dTable.Columns.Add("RandomNo")
dTable.Columns.Add("Date")
dTable.Columns.Add("Time")

For n As Integer = 0 To 9
dRow = dTable.NewRow()
dTime = DateTime.Now

dRow("IsChecked") = "false"
dRow("RandomNo") = rnd.NextDouble()
dRow("Date") = dTime.ToString("MM/dd/yyyy")
dRow("Time") = dTime.ToString("hh:mm:ss tt")

dTable.Rows.Add(dRow)
dTable.AcceptChanges()
Next

Return dTable
End Function

Private Sub dgvSelectAll_CellValueChanged(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)
If Not IsHeaderCheckBoxClicked Then
RowCheckBoxClick(DirectCast(dgvSelectAll(e.ColumnIndex, e.RowIndex), DataGridViewCheckBoxCell))
End If
End Sub

Private Sub dgvSelectAll_CurrentCellDirtyStateChanged(ByVal sender As Object, ByVal e As EventArgs)
If TypeOf dgvSelectAll.CurrentCell Is DataGridViewCheckBoxCell Then
dgvSelectAll.CommitEdit(DataGridViewDataErrorContexts.Commit)
End If
End Sub

Private Sub HeaderCheckBox_MouseClick(ByVal sender As Object, ByVal e As MouseEventArgs)
HeaderCheckBoxClick(DirectCast(sender, CheckBox))
End Sub

Private Sub HeaderCheckBox_KeyUp(ByVal sender As Object, ByVal e As KeyEventArgs)
If e.KeyCode = Keys.Space Then
HeaderCheckBoxClick(DirectCast(sender, CheckBox))
End If
End Sub

Private Sub dgvSelectAll_CellPainting(ByVal sender As Object, ByVal e As DataGridViewCellPaintingEventArgs)
If e.RowIndex = -1 AndAlso e.ColumnIndex = 0 Then
ResetHeaderCheckBoxLocation(e.ColumnIndex, e.RowIndex)
End If
End Sub

Private Sub AddHeaderCheckBox()
HeaderCheckBox = New CheckBox()

HeaderCheckBox.Size = New Size(15, 15)

'Add the CheckBox into the DataGridView
Me.dgvSelectAll.Controls.Add(HeaderCheckBox)
End Sub

Private Sub ResetHeaderCheckBoxLocation(ByVal ColumnIndex As Integer, ByVal RowIndex As Integer)
'Get the column header cell bounds
Dim oRectangle As Rectangle = Me.dgvSelectAll.GetCellDisplayRectangle(ColumnIndex, RowIndex, True)

Dim oPoint As New Point()

oPoint.X = oRectangle.Location.X + (oRectangle.Width - HeaderCheckBox.Width) / 2 + 1
oPoint.Y = oRectangle.Location.Y + (oRectangle.Height - HeaderCheckBox.Height) / 2 + 1

'Change the location of the CheckBox to make it stay on the header
HeaderCheckBox.Location = oPoint
End Sub

Private Sub HeaderCheckBoxClick(ByVal HCheckBox As CheckBox)
IsHeaderCheckBoxClicked = True

For Each Row As DataGridViewRow In dgvSelectAll.Rows
DirectCast(Row.Cells("chkBxSelect"), DataGridViewCheckBoxCell).Value = HCheckBox.Checked
Next

dgvSelectAll.RefreshEdit()

TotalCheckedCheckBoxes = If(HCheckBox.Checked, TotalCheckBoxes, 0)

IsHeaderCheckBoxClicked = False
End Sub

Private Sub RowCheckBoxClick(ByVal RCheckBox As DataGridViewCheckBoxCell)
If RCheckBox IsNot Nothing Then
'Modifiy Counter;
If CBool(RCheckBox.Value) AndAlso TotalCheckedCheckBoxes < TotalCheckBoxes Then
TotalCheckedCheckBoxes += 1
ElseIf TotalCheckedCheckBoxes > 0 Then
TotalCheckedCheckBoxes -= 1
End If

'Change state of the header CheckBox.
If TotalCheckedCheckBoxes < TotalCheckBoxes Then
HeaderCheckBox.Checked = False
ElseIf TotalCheckedCheckBoxes = TotalCheckBoxes Then
HeaderCheckBox.Checked = True
End If
End If
End Sub
End Class
End Namespace

No comments:

Post a Comment