以下方法在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