以下方法在VBNET2008中通过验证:
a,新一个类库
b,类库属性应用程序类型更改为:Windows应用程序,启动窗体设置为DataGridViewCheckBox.Form1
Imports System
Imports System.Data
Imports System.Data.sqlClient
Imports System.Drawing
Imports System.Windows.forms
Namespace DataGridViewCheckBox
@H_404_52@Public @H_404_52@Class Form1
@H_404_52@Inherits Form
@H_404_52@Friend @H_404_52@WithEvents Label1 @H_404_52@As System.Windows.Forms.Label
@H_404_52@Friend @H_404_52@WithEvents DataGridView1 @H_404_52@As System.Windows.Forms.DataGridView
@H_404_52@Private @H_404_52@Sub DataGridViewCheckBoxHeaderCellShow()
@H_404_52@Dim checkBox1 @H_404_52@As @H_404_52@New DataGridCheckBoxHeaderCell '定义一眉头CheckBox1从类DataGridCheckBoxHeaderCell构造而来
@H_404_52@Dim checkBoxColumn @H_404_52@As @H_404_52@New DataGridViewCheckBoxColumn '定义一个新列
checkBoxColumn.HeaderCell = checkBox1 '新列增加一个控件CheckBox1
checkBoxColumn.HeaderCell.Value = "全选" '列头显示全选字符串
@H_404_52@Me.DataGridView1.Columns.Add(checkBoxColumn) 'DataGridView1 新增0列并有checkBox属性
@H_404_52@Private @H_404_52@Sub CheckBox_OnCheckBoxClicked(@H_404_52@ByVal ander @H_404_52@As @H_404_52@Boolean)
@H_404_52@For @H_404_52@Each Row @H_404_52@As DataGridViewRow @H_404_52@In @H_404_52@Me.DataGridView1.Rows
@H_404_52@If DataGridCheckBoxHeaderCell.IsChecked @H_404_52@Then
Row.Cells(0).Value = @H_404_52@True
@H_404_52@Me.DataGridView1.Columns(0).HeaderText = "全不选"
@H_404_52@Else
Row.Cells(0).Value = @H_404_52@False
@H_404_52@Me.DataGridView1.Columns(0).HeaderText = "全选"
@H_404_52@Next
@H_404_52@Public @H_404_52@Sub @H_404_52@New()
InitializeComponent()
@H_404_52@Private @H_404_52@Sub InitializeComponent()
@H_404_52@Me.DataGridView1 = @H_404_52@New System.Windows.Forms.DataGridView
@H_404_52@Me.Label1 = @H_404_52@New System.Windows.Forms.Label
@H_404_52@CType(@H_404_52@Me.DataGridView1,System.ComponentModel.ISupportInitialize).BeginInit()
@H_404_52@Me.SuspendLayout()
'
'DataGridView1
'
@H_404_52@Me.DataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
@H_404_52@Me.DataGridView1.Location = @H_404_52@New System.Drawing.Point(12,12)
@H_404_52@Me.DataGridView1.Name = "DataGridView1"
@H_404_52@Me.DataGridView1.RowTemplate.Height = 23
@H_404_52@Me.DataGridView1.Size = @H_404_52@New System.Drawing.Size(457,230)
@H_404_52@Me.DataGridView1.TabIndex = 0
'
'Label1
'
@H_404_52@Me.Label1.AutoSize = @H_404_52@True
@H_404_52@Me.Label1.Location = @H_404_52@New System.Drawing.Point(10,245)
@H_404_52@Me.Label1.Name = "Label1"
@H_404_52@Me.Label1.Size = @H_404_52@New System.Drawing.Size(77,12)
@H_404_52@Me.Label1.TabIndex = 1
@H_404_52@Me.Label1.Text = "点击列头选择"
'
'Form1
'
@H_404_52@Me.ClientSize = @H_404_52@New System.Drawing.Size(481,265)
@H_404_52@Me.Controls.Add(@H_404_52@Me.Label1)
@H_404_52@Me.Controls.Add(@H_404_52@Me.DataGridView1)
@H_404_52@Me.Name = "Form1"
@H_404_52@CType(@H_404_52@Me.DataGridView1,System.ComponentModel.ISupportInitialize).EndInit()
@H_404_52@Me.ResumeLayout(@H_404_52@False)
@H_404_52@Me.PerformLayout()
@H_404_52@Private @H_404_52@Sub Form1_Load(@H_404_52@ByVal sender @H_404_52@As System.Object,@H_404_52@ByVal e @H_404_52@As System.EventArgs) @H_404_52@Handles @H_404_52@MyBase.Load
DataGridViewCheckBoxHeaderCellShow()
@H_404_52@Dim con @H_404_52@As @H_404_52@New sqlConnection("server=(local);uid=sa;pwd=sa;database=pubs")
@H_404_52@Dim cad @H_404_52@As @H_404_52@New sqlDataAdapter("select * from authors",con)
@H_404_52@Dim TAB @H_404_52@As @H_404_52@New DataSet
cad.Fill(TAB)
@H_404_52@Me.DataGridView1.DataSource = TAB.Tables(0).DefaultView
TAB.Dispose()
cad.Dispose()
con.Close()
DataGridView1.AllowUserToAddRows = @H_404_52@False '最下面的新行不显示
@H_404_52@Private @H_404_52@Sub DataGridView1_ColumnHeaderMouseClick(@H_404_52@ByVal sender @H_404_52@As @H_404_52@Object,@H_404_52@ByVal e @H_404_52@As System.Windows.Forms.DataGridViewCellMouseEventArgs) @H_404_52@Handles DataGridView1.ColumnHeaderMouseClick
@H_404_52@Me.Label1.Focus()
@H_404_52@Dim Click1 @H_404_52@As DataGridCheckBoxHeaderCell.DataGridViewCheckBoxClickedHandler
Click1 = @H_404_52@New DataGridCheckBoxHeaderCell.DataGridViewCheckBoxClickedHandler(@H_404_52@AddressOf CheckBox_OnCheckBoxClicked)
Click1.Invoke(@H_404_52@True)
REM 或 AddHandler DataGridCheckBoxHeaderCell.OnBooland,AddressOf CheckBox_OnCheckBoxClicked
@H_404_52@Public @H_404_52@Class DataGridCheckBoxHeaderCellEventArgs
@H_404_52@Inherits EventArgs '由 EventArgs继承
@H_404_52@Public @H_404_52@Shared _bChecked @H_404_52@As @H_404_52@Boolean
@H_404_52@Public @H_404_52@Sub @H_404_52@New()
@H_404_52@Public @H_404_52@Sub @H_404_52@New(@H_404_52@ByVal bChecked @H_404_52@As @H_404_52@Boolean) '构造函数
_bChecked = bChecked
@H_404_52@Protected @H_404_52@Shared @H_404_52@Shadows @H_404_52@Property Checked() @H_404_52@As @H_404_52@Boolean
@H_404_52@Get
@H_404_52@If _bChecked = @H_404_52@True @H_404_52@Then _bChecked = @H_404_52@True
@H_404_52@Return _bChecked
@H_404_52@Set(@H_404_52@ByVal value @H_404_52@As @H_404_52@Boolean)
_bChecked = value
@H_404_52@End @H_404_52@Property
@H_404_52@Public @H_404_52@Class DataGridCheckBoxHeaderCell
@H_404_52@Inherits DataGridViewColumnHeaderCell 'DataGridViewCheckBoxHeaderCell 由 DataGridViewColumnHeaderCell继承而来
@H_404_52@Public @H_404_52@Delegate @H_404_52@Sub DataGridViewCheckBoxClickedHandler(@H_404_52@ByVal state @H_404_52@As @H_404_52@Boolean) ' 委托处理DataGridViewCheckBoxClickedHandler 事件
@H_404_52@Private checkBoxLocation @H_404_52@As Point
@H_404_52@Public checkBoxSize @H_404_52@As Size
@H_404_52@Public @H_404_52@Shared _checked @H_404_52@As @H_404_52@Boolean
@H_404_52@Private _cellLocation @H_404_52@As @H_404_52@New Point()
@H_404_52@Private _cbState @H_404_52@As System.Windows.Forms.VisualStyles.CheckBoxState = System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal
@H_404_52@Public @H_404_52@Shared @H_404_52@Event OnBooland(@H_404_52@ByVal e @H_404_52@As @H_404_52@Boolean)
@H_404_52@Public @H_404_52@Event OnCheckBoxClicked @H_404_52@As DataGridViewCheckBoxClickedHandler
'
REM '重载重写DataGridvewOnCheckBoxClicked方法 由OnCheckBoxClicked引发
@H_404_52@Protected @H_404_52@Overridable @H_404_52@Overloads @H_404_52@Sub DataGridvewOnCheckBoxClicked(@H_404_52@ByVal state @H_404_52@As @H_404_52@Boolean)
@H_404_52@RaiseEvent OnCheckBoxClicked(state)
@H_404_52@RaiseEvent OnBooland(_checked)
REM 定义共享IsChecked属性
@H_404_52@Public @H_404_52@Shared @H_404_52@Property IsChecked() @H_404_52@As @H_404_52@Boolean
@H_404_52@Get
@H_404_52@Return _checked
@H_404_52@Set(@H_404_52@ByVal value @H_404_52@As @H_404_52@Boolean)
_checked = value
@H_404_52@RaiseEvent OnBooland(_checked)
@H_404_52@End @H_404_52@Property
'重写Paint方法
@H_404_52@Protected @H_404_52@Overloads @H_404_52@Overrides @H_404_52@Sub Paint(@H_404_52@ByVal graphics @H_404_52@As System.Drawing.Graphics,@H_404_52@ByVal clipBounds @H_404_52@As System.Drawing.Rectangle,@H_404_52@ByVal cellBounds @H_404_52@As System.Drawing.Rectangle,_
@H_404_52@ByVal rowIndex @H_404_52@As @H_404_52@Integer,@H_404_52@ByVal dataGridViewElementState @H_404_52@As DataGridViewElementStates,@H_404_52@ByVal value @H_404_52@As @H_404_52@Object,_
@H_404_52@ByVal formattedValue @H_404_52@As @H_404_52@Object,@H_404_52@ByVal errorText @H_404_52@As @H_404_52@String,@H_404_52@ByVal cellStyle @H_404_52@As DataGridViewCellStyle,_
@H_404_52@ByVal advancedBorderStyle @H_404_52@As DataGridViewAdvancedBorderStyle,@H_404_52@ByVal paintParts @H_404_52@As DataGridViewPaintParts)
@H_404_52@MyBase.Paint(graphics,clipBounds,cellBounds,rowIndex,dataGridViewElementState,value,_
formattedValue,errorText,cellStyle,advancedBorderStyle,paintParts)
@H_404_52@Dim p @H_404_52@As @H_404_52@New Point()
@H_404_52@Dim s @H_404_52@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
@H_404_52@If _checked @H_404_52@Then
_cbState = System.Windows.Forms.VisualStyles.CheckBoxState.CheckedNormal
@H_404_52@Else
_cbState = System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal
CheckBoxRenderer.DrawCheckBox(graphics,checkBoxLocation,_cbState)
REM 重写OnMouseClick方法 点击列头checkBox单击事件
@H_404_52@Protected @H_404_52@Overloads @H_404_52@Overrides @H_404_52@Sub OnMouseClick(@H_404_52@ByVal e @H_404_52@As DataGridViewCellMouseEventArgs)
@H_404_52@Dim p @H_404_52@As @H_404_52@New Point(e.X + _cellLocation.X,e.Y + _cellLocation.Y)
@H_404_52@If p.X >= checkBoxLocation.X @H_404_52@AndAlso p.X <= checkBoxLocation.X + checkBoxSize.Width @H_404_52@AndAlso p.Y >= checkBoxLocation.Y @H_404_52@AndAlso p.Y <= checkBoxLocation.Y + checkBoxSize.Height @H_404_52@Then
_checked = @H_404_52@Not _checked
DataGridvewOnCheckBoxClicked(_checked)
@H_404_52@Me.DataGridView.InvalidateCell(@H_404_52@Me)
@H_404_52@MyBase.OnMouseClick(e)
End @H_404_52@Namespace
@H_404_52@
以上不足之处,请见谅