VB.NET: DataGridView列头实现"全选"和"全不选"功能

以下方法在VBNET2008中通过验证:

a,新一个类库

b,类库属性应用程序类型更改为:Windows应用程序,启动窗体设置为DataGridViewCheckbox.Form1

下面为自定义类代码

Imports System

Imports System.Data

Imports System.Data.SqlClient

Imports System.Drawing

Imports System.Windows.forms

Namespace DataGridViewCheckBox

Public Class Form1

Inherits Form

Friend WithEvents Label1 As System.Windows.Forms.Label

Friend WithEvents DataGridView1 As System.Windows.Forms.DataGridView

Private Sub DataGridViewCheckboxHeaderCellShow()

Dim checkbox1 As New DataGridCheckBoxHeaderCell '定义一眉头Checkbox1从类DataGridCheckBoxHeaderCell构造而来

Dim checkboxColumn As New DataGridViewCheckBoxColumn '定义一个新列

checkboxColumn.HeaderCell = checkbox1 '新列增加一个控件Checkbox1

checkboxColumn.HeaderCell.Value = "全选" '列头显示全选字符串

Me.DataGridView1.Columns.Add(checkboxColumn) 'DataGridView1 新增0列并有checkbox属性

End Sub

Private Sub Checkbox_OnCheckboxClicked(ByVal ander As Boolean)

For Each Row As DataGridViewRow In Me.DataGridView1.Rows

If DataGridCheckBoxHeaderCell.IsChecked Then

Row.Cells(0).Value = True

Me.DataGridView1.Columns(0).HeaderText = "全不选"

Else

Row.Cells(0).Value = False

Me.DataGridView1.Columns(0).HeaderText = "全选"

End If

Next

End Sub

Public Sub New()

InitializeComponent()

End Sub

Private Sub InitializeComponent()

Me.DataGridView1 = New System.Windows.Forms.DataGridView

Me.Label1 = New System.Windows.Forms.Label

CType(Me.DataGridView1,System.ComponentModel.ISupportInitialize).BeginInit()

Me.SuspendLayout()

'

'DataGridView1

'

Me.DataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize

Me.DataGridView1.Location = New System.Drawing.Point(12,12)

Me.DataGridView1.Name = "DataGridView1"

Me.DataGridView1.RowTemplate.Height = 23

Me.DataGridView1.Size = New System.Drawing.Size(457,230)

Me.DataGridView1.TabIndex = 0

'

'Label1

'

Me.Label1.AutoSize = True

Me.Label1.Location = New System.Drawing.Point(10,245)

Me.Label1.Name = "Label1"

Me.Label1.Size = New System.Drawing.Size(77,12)

Me.Label1.TabIndex = 1

Me.Label1.Text = "点击列头选择"

'

'Form1

'

Me.ClientSize = New System.Drawing.Size(481,265)

Me.Controls.Add(Me.Label1)

Me.Controls.Add(Me.DataGridView1)

Me.Name = "Form1"

CType(Me.DataGridView1,System.ComponentModel.ISupportInitialize).EndInit()

Me.ResumeLayout(False)

Me.PerformLayout()

End Sub

Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Load

DataGridViewCheckboxHeaderCellShow()

Dim con As New SqlConnection("server=(local);uid=sa;pwd=sa;database=pubs")

Dim cad As New SqlDataAdapter("select * from authors",con)

Dim TAB As New DataSet

cad.Fill(TAB)

Me.DataGridView1.DataSource = TAB.Tables(0).DefaultView

TAB.Dispose()

cad.Dispose()

con.Close()

DataGridView1.AllowUserToAddRows = False '最下面的新行不显示

End Sub

Private Sub DataGridView1_ColumnHeaderMouseClick(ByVal sender As Object,ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.ColumnHeaderMouseClick

Me.Label1.Focus()

Dim Click1 As DataGridCheckBoxHeaderCell.DataGridViewCheckBoxClickedHandler

Click1 = New DataGridCheckBoxHeaderCell.DataGridViewCheckBoxClickedHandler(AddressOf Checkbox_OnCheckboxClicked)

Click1.Invoke(True)

REM AddHandler DataGridCheckBoxHeaderCell.OnBooland,AddressOf Checkbox_OnCheckboxClicked

End Sub

End Class

Public Class DataGridCheckBoxHeaderCellEventArgs

Inherits EventArgs ' EventArgs继承

Public Shared _bChecked As Boolean

Public Sub New()

End Sub

Public Sub New(ByVal bChecked As Boolean) '构造函数

_bChecked = bChecked

End Sub

Protected Shared Shadows Property Checked() As Boolean

Get

If _bChecked = True Then _bChecked = True

Return _bChecked

End Get

Set(ByVal value As Boolean)

_bChecked = value

End Set

End Property

End Class

Public Class DataGridCheckBoxHeaderCell

Inherits DataGridViewColumnHeaderCell 'DataGridViewCheckBoxHeaderCell DataGridViewColumnHeaderCell继承而来

Public Delegate Sub DataGridViewCheckBoxClickedHandler(ByVal state As Boolean) ' 委托处理DataGridViewCheckBoxClickedHandler 事件

Private checkBoxLocation As Point

Public checkBoxSize As Size

Public Shared _checked As Boolean

Private _cellLocation As New Point()

Private _cbState As System.Windows.Forms.VisualStyles.CheckBoxState = System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal

Public Shared Event OnBooland(ByVal e As Boolean)

Public Event OnCheckBoxClicked As DataGridViewCheckBoxClickedHandler

'

REM '重载重写DataGridvewOnCheckBoxClicked方法 OnCheckBoxClicked引发

Protected Overridable Overloads Sub DataGridvewOnCheckBoxClicked(ByVal state As Boolean)

RaiseEvent OnCheckBoxClicked(state)

RaiseEvent OnBooland(_checked)

End Sub

REM 定义共享IsChecked属性

Public Shared Property IsChecked() As Boolean

Get

Return _checked

End Get

Set(ByVal value As Boolean)

_checked = value

RaiseEvent OnBooland(_checked)

End Set

End Property

'重写Paint方法

Protected Overloads Overrides Sub Paint(ByVal graphics As System.Drawing.Graphics,ByVal clipBounds As System.Drawing.Rectangle,ByVal cellBounds As System.Drawing.Rectangle,_

ByVal rowIndex As Integer,ByVal dataGridViewElementState As DataGridViewElementStates,ByVal value As Object,_

ByVal formattedValue As Object,ByVal errorText As String,ByVal cellStyle As DataGridViewCellStyle,_

ByVal advancedBorderStyle As DataGridViewAdvancedBorderStyle,ByVal paintParts As DataGridViewPaintParts)

MyBase.Paint(graphics,clipBounds,cellBounds,rowIndex,dataGridViewElementState,value,_

formattedValue,errorText,cellStyle,advancedBorderStyle,paintParts)

Dim p As New Point()

Dim s As Size = CheckBoxRenderer.GetGlyphSize(graphics,System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal)

p.X = cellBounds.Location.X + (cellBounds.Width / 2) - (s.Width / 2)

p.Y = cellBounds.Location.Y + (cellBounds.Height / 2) - (s.Height / 2)

_cellLocation = cellBounds.Location

checkBoxLocation = p

checkBoxSize = s

If _checked Then

_cbState = System.Windows.Forms.VisualStyles.CheckBoxState.CheckedNormal

Else

_cbState = System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal

End If

CheckBoxRenderer.DrawCheckBox(graphics,checkBoxLocation,_cbState)

End Sub

REM 重写OnMouseClick方法 点击列头checkbox单击事件

Protected Overloads Overrides Sub OnMouseClick(ByVal e As DataGridViewCellMouseEventArgs)

Dim p As New Point(e.X + _cellLocation.X,e.Y + _cellLocation.Y)

If p.X >= checkBoxLocation.X AndAlso p.X <= checkBoxLocation.X + checkBoxSize.Width AndAlso p.Y >= checkBoxLocation.Y AndAlso p.Y <= checkBoxLocation.Y + checkBoxSize.Height Then

_checked = Not _checked

DataGridvewOnCheckBoxClicked(_checked)

Me.DataGridView.InvalidateCell(Me)

End If

MyBase.OnMouseClick(e)

End Sub

End Class

End Namespace

以上不足之处,请见谅

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...