C# Winform中DataGridView 设置全选/取消
2013-11-26 17:48
246 查看
public partial
class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private
void button1_Click(object sender, EventArgs e)
{
DataTable dt =
new DataTable();
DataColumn column;
for (int i
= 0; i
< 4; i++)
{
if (i
== 3)
{
column =
new DataColumn();
column.ColumnName =
"column"
+ i;
column.DataType =
typeof(Boolean);
dt.Columns.Add(column);
dataGridView1.DataSource = dt;
}
else
{
column =
new DataColumn();
column.ColumnName =
"column"
+ i;
column.DataType =
typeof(string);
dt.Columns.Add(column);
}
}
for (int i
= 0; i
< 4; i++)
{
DataRow row = dt.NewRow();
for (int j
= 0; j
< 4; j++)
{
if (j
== 3)
{
row[j] =
false;
}
else
{
row[j] = i.ToString();
}
}
dt.Rows.Add(row);
}
dataGridView1.DataSource = dt;
datagridviewCheckboxHeaderCell ch =
new datagridviewCheckboxHeaderCell();
ch.OnCheckBoxClicked +=
new datagridviewcheckboxHeaderEventHander(ch_OnCheckBoxClicked);
//第四列为DataGridViewCheckBoxColumn
DataGridViewCheckBoxColumn checkboxCol
= this.dataGridView1.Columns[3]
as DataGridViewCheckBoxColumn;
checkboxCol.HeaderCell = ch;
checkboxCol.HeaderCell.Value =
string.Empty;//消除列头checkbox旁出现的文字
}
void ch_OnCheckBoxClicked(object sender, Form1.datagridviewCheckboxHeaderEventArgs e)
{
foreach (DataGridViewRow dgvRow
in this.dataGridView1.Rows)
{
if (e.CheckedState)
{
dgvRow.Cells[3].Value
= true;
}
else
{
dgvRow.Cells[3].Value
= false;
}
}
}
//定义触发单击事件的委托
public
delegate
void datagridviewcheckboxHeaderEventHander(object sender, datagridviewCheckboxHeaderEventArgs e);
//定义包含列头checkbox选择状态的参数类
class datagridviewCheckboxHeaderEventArgs : EventArgs
{
private
bool checkedState
= false;
public
bool CheckedState
{
get {
return checkedState; }
set { checkedState
= value; }
}
}
//定义继承于DataGridViewColumnHeaderCell的类,用于绘制checkbox,定义checkbox鼠标单击事件
class datagridviewCheckboxHeaderCell : DataGridViewColumnHeaderCell
{
Point checkBoxLocation;
Size checkBoxSize;
bool _checked
= false;
Point _cellLocation =
new Point();
System.Windows.Forms.VisualStyles.CheckBoxState _cbState =
System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal;
public
event datagridviewcheckboxHeaderEventHander OnCheckBoxClicked;
//绘制列头checkbox
protected
override
void Paint(System.Drawing.Graphics graphics,
System.Drawing.Rectangle clipBounds,
System.Drawing.Rectangle cellBounds,
int rowIndex,
DataGridViewElementStates dataGridViewElementState,
object value,
object formattedValue,
string errorText,
DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts)
{
base.Paint(graphics, clipBounds, cellBounds, rowIndex,
dataGridViewElementState, value,
formattedValue, errorText, cellStyle,
advancedBorderStyle, paintParts);
Point p =
new Point();
Size s = CheckBoxRenderer.GetGlyphSize(graphics,
System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal);
p.X = cellBounds.Location.X
+
(cellBounds.Width /
2) - (s.Width
/ 2)
- 1;//列头checkbox的X坐标
p.Y = cellBounds.Location.Y
+
(cellBounds.Height /
2) - (s.Height
/ 2);//列头checkbox的Y坐标
_cellLocation = cellBounds.Location;
checkBoxLocation = p;
checkBoxSize = s;
if (_checked)
_cbState = System.Windows.Forms.VisualStyles.
CheckBoxState.CheckedNormal;
else
_cbState = System.Windows.Forms.VisualStyles.
CheckBoxState.UncheckedNormal;
CheckBoxRenderer.DrawCheckBox
(graphics, checkBoxLocation, _cbState);
}
///
<summary>
/// 点击列头checkbox单击事件
///
</summary>
protected
override
void OnMouseClick(DataGridViewCellMouseEventArgs e)
{
Point p =
new Point(e.X
+ _cellLocation.X, e.Y
+ _cellLocation.Y);
if (p.X
>= checkBoxLocation.X
&& p.X
<=
checkBoxLocation.X + checkBoxSize.Width
&& p.Y
>= checkBoxLocation.Y
&& p.Y
<=
checkBoxLocation.Y + checkBoxSize.Height)
{
_checked =
!_checked;
//获取列头checkbox的选择状态
datagridviewCheckboxHeaderEventArgs ex
= new datagridviewCheckboxHeaderEventArgs();
ex.CheckedState = _checked;
object sender
= new
object();//此处不代表选择的列头checkbox,只是作为参数传递。应该列头checkbox是绘制出来的,无法获得它的实例
if (OnCheckBoxClicked
!= null)
{
OnCheckBoxClicked(sender, ex);//触发单击事件
this.DataGridView.InvalidateCell(this);
}
}
base.OnMouseClick(e);
}
}
}//end class
class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private
void button1_Click(object sender, EventArgs e)
{
DataTable dt =
new DataTable();
DataColumn column;
for (int i
= 0; i
< 4; i++)
{
if (i
== 3)
{
column =
new DataColumn();
column.ColumnName =
"column"
+ i;
column.DataType =
typeof(Boolean);
dt.Columns.Add(column);
dataGridView1.DataSource = dt;
}
else
{
column =
new DataColumn();
column.ColumnName =
"column"
+ i;
column.DataType =
typeof(string);
dt.Columns.Add(column);
}
}
for (int i
= 0; i
< 4; i++)
{
DataRow row = dt.NewRow();
for (int j
= 0; j
< 4; j++)
{
if (j
== 3)
{
row[j] =
false;
}
else
{
row[j] = i.ToString();
}
}
dt.Rows.Add(row);
}
dataGridView1.DataSource = dt;
datagridviewCheckboxHeaderCell ch =
new datagridviewCheckboxHeaderCell();
ch.OnCheckBoxClicked +=
new datagridviewcheckboxHeaderEventHander(ch_OnCheckBoxClicked);
//第四列为DataGridViewCheckBoxColumn
DataGridViewCheckBoxColumn checkboxCol
= this.dataGridView1.Columns[3]
as DataGridViewCheckBoxColumn;
checkboxCol.HeaderCell = ch;
checkboxCol.HeaderCell.Value =
string.Empty;//消除列头checkbox旁出现的文字
}
void ch_OnCheckBoxClicked(object sender, Form1.datagridviewCheckboxHeaderEventArgs e)
{
foreach (DataGridViewRow dgvRow
in this.dataGridView1.Rows)
{
if (e.CheckedState)
{
dgvRow.Cells[3].Value
= true;
}
else
{
dgvRow.Cells[3].Value
= false;
}
}
}
//定义触发单击事件的委托
public
delegate
void datagridviewcheckboxHeaderEventHander(object sender, datagridviewCheckboxHeaderEventArgs e);
//定义包含列头checkbox选择状态的参数类
class datagridviewCheckboxHeaderEventArgs : EventArgs
{
private
bool checkedState
= false;
public
bool CheckedState
{
get {
return checkedState; }
set { checkedState
= value; }
}
}
//定义继承于DataGridViewColumnHeaderCell的类,用于绘制checkbox,定义checkbox鼠标单击事件
class datagridviewCheckboxHeaderCell : DataGridViewColumnHeaderCell
{
Point checkBoxLocation;
Size checkBoxSize;
bool _checked
= false;
Point _cellLocation =
new Point();
System.Windows.Forms.VisualStyles.CheckBoxState _cbState =
System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal;
public
event datagridviewcheckboxHeaderEventHander OnCheckBoxClicked;
//绘制列头checkbox
protected
override
void Paint(System.Drawing.Graphics graphics,
System.Drawing.Rectangle clipBounds,
System.Drawing.Rectangle cellBounds,
int rowIndex,
DataGridViewElementStates dataGridViewElementState,
object value,
object formattedValue,
string errorText,
DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts)
{
base.Paint(graphics, clipBounds, cellBounds, rowIndex,
dataGridViewElementState, value,
formattedValue, errorText, cellStyle,
advancedBorderStyle, paintParts);
Point p =
new Point();
Size s = CheckBoxRenderer.GetGlyphSize(graphics,
System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal);
p.X = cellBounds.Location.X
+
(cellBounds.Width /
2) - (s.Width
/ 2)
- 1;//列头checkbox的X坐标
p.Y = cellBounds.Location.Y
+
(cellBounds.Height /
2) - (s.Height
/ 2);//列头checkbox的Y坐标
_cellLocation = cellBounds.Location;
checkBoxLocation = p;
checkBoxSize = s;
if (_checked)
_cbState = System.Windows.Forms.VisualStyles.
CheckBoxState.CheckedNormal;
else
_cbState = System.Windows.Forms.VisualStyles.
CheckBoxState.UncheckedNormal;
CheckBoxRenderer.DrawCheckBox
(graphics, checkBoxLocation, _cbState);
}
///
<summary>
/// 点击列头checkbox单击事件
///
</summary>
protected
override
void OnMouseClick(DataGridViewCellMouseEventArgs e)
{
Point p =
new Point(e.X
+ _cellLocation.X, e.Y
+ _cellLocation.Y);
if (p.X
>= checkBoxLocation.X
&& p.X
<=
checkBoxLocation.X + checkBoxSize.Width
&& p.Y
>= checkBoxLocation.Y
&& p.Y
<=
checkBoxLocation.Y + checkBoxSize.Height)
{
_checked =
!_checked;
//获取列头checkbox的选择状态
datagridviewCheckboxHeaderEventArgs ex
= new datagridviewCheckboxHeaderEventArgs();
ex.CheckedState = _checked;
object sender
= new
object();//此处不代表选择的列头checkbox,只是作为参数传递。应该列头checkbox是绘制出来的,无法获得它的实例
if (OnCheckBoxClicked
!= null)
{
OnCheckBoxClicked(sender, ex);//触发单击事件
this.DataGridView.InvalidateCell(this);
}
}
base.OnMouseClick(e);
}
}
}//end class
相关文章推荐
- js checkbox全选 反选 取消全部设置表单html复选框勾选
- c# WinForm开发 DataGridView控件的各种操作总结(单元格操作,属性设置)
- js checkbox全选 反选 取消全部设置表单checkbox复选框勾选
- c# WinForm开发 DataGridView控件的各种操作总结(单元格操作,属性设置)
- c# WinForm开发 DataGridView控件的各种操作总结(单元格操作,属性设置)
- WinForm中DataGridView的全选与取消全选
- C# WinForm 取消DataGridView的默认选中Cell 使其不反蓝
- DataGridView添加一行数据、全选、取消全选、清空数据、删除选中行
- c# WinForm开发 DataGridView控件的各种操作总结(单元格操作,属性设置)
- DevExpress中GridControl的属性设置及动态绑定数据和全选取消全选
- c# WinForm开发 DataGridView控件的各种操作总结(单元格操作,属性设置)
- C# WinForm 取消DataGridView的默认选中Cell 使其不反蓝
- c# WinForm开发 DataGridView控件的各种操作总结(单元格操作,属性设置)
- c# WinForm开发 DataGridView控件的各种操作总结(单元格操作,属性设置)
- datagridview里面的checkbox全选和取消全选
- DataGridView里的Checkbox全选问题解决了!
- 扩展GridView控件(4) - 联动复选框(复选框的全选和取消全选)
- table 里面的 checkbox 全选,取消,单选,选中一行后选中行高亮
- gcc的-D和-U参数:宏的设置与取消