C# winfrom 中datagridview中checkbox的使用方法

众所周知,datagridview控件是CS架构中用的比较频繁的一个控件,里面提供了checkbox列的功能,可是却没有在列头给出checkbox控件用于全选/全部取消所有行的功能,确实是个遗憾,这里就通过绘制实现这个功能.

效果图:

C# winfrom 中datagridview中checkbox的使用方法_第1张图片

一.创建一个表,里面包含bit字段,datagridview的DataGridViewCheckBoxColumn列会自动将其转换成checkbox列

二. 界面上调用

DataGridView绑定完数据以后,在第一列绘制CheckBox列头,可以在DataBindingComplete事件中处理

grid.DataBindingAfter += new Action(DataBindingAterEventHandler);

C# winfrom 中datagridview中checkbox的使用方法_第2张图片

处理列头全选事件

C# winfrom 中datagridview中checkbox的使用方法_第3张图片

行多选处理

C# winfrom 中datagridview中checkbox的使用方法_第4张图片

实现核心代码

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Drawing;

using System.Windows.Forms;

using System.Windows.Forms.VisualStyles;

namespace YXSY.UI.UICommon

{

///

/// 功能描述:在DataGridView列头绘制CheckBox控件

///

///

/// 创建日期:2010-12-03, 作者:Huang.Ting

///

//实现全选的事件参数类

public class DataGridviewCheckboxHeaderEventHander : EventArgs

{

private bool checkedState = false;

public bool CheckedState

{

get { return checkedState; }

set { checkedState = value; }

}

}

//与事件关联的委托

public delegate void DataGridviewCheckboxHeaderCellEventHander(object sender, DataGridviewCheckboxHeaderEventHander e);

//重写 DataGridViewColumnHeaderCell

public class DataGridviewCheckboxHeaderCell : DataGridViewColumnHeaderCell

{

private Point checkBoxLocation;

private Size checkBoxSize;

private bool isChecked = false;

private Point cellLocation = new Point();

private CheckBoxState cbState = CheckBoxState.UncheckedNormal;

public event DataGridviewCheckboxHeaderCellEventHander OnCheckBoxClicked;

//绘制列头checkbox

protected override void Paint(Graphics g,

Rectangle clipBounds,

Rectangle cellBounds,

int rowIndex,

DataGridViewElementStates dataGridViewElementState,

object value,

object formattedValue,

string errorText,

DataGridViewCellStyle cellStyle,

DataGridViewAdvancedBorderStyle advancedBorderStyle,

DataGridViewPaintParts paintParts)

{

base.Paint(g, clipBounds, cellBounds, rowIndex,dataGridViewElementState, value,formattedValue, errorText, cellStyle,advancedBorderStyle, paintParts);

Point p = new Point();

Size s = CheckBoxRenderer.GetGlyphSize(g, CheckBoxState.UncheckedNormal);

//列头checkbox的X坐标

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

//列头checkbox的Y坐标

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

cellLocation = cellBounds.Location;

checkBoxLocation = p;

checkBoxSize = s;

if (isChecked)

cbState = CheckBoxState.CheckedNormal;

else

cbState =  CheckBoxState.UncheckedNormal;

//绘制复选框

CheckBoxRenderer.DrawCheckBox(g, checkBoxLocation, cbState);

}

///

/// 响应点击列头checkbox单击事件

///

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)

{

isChecked = !isChecked;

//获取列头checkbox的选择状态

DataGridviewCheckboxHeaderEventHander ex = new DataGridviewCheckboxHeaderEventHander();

ex.CheckedState = isChecked;

//此处不代表选择的列头checkbox,只是作为参数传递。应该列头checkbox是绘制出来的,无法获得它的实例

object sender = new object();

if (OnCheckBoxClicked != null)

{

//触发单击事件

OnCheckBoxClicked(sender, ex);

this.DataGridView.InvalidateCell(this);

}

}

base.OnMouseClick(e);

}

}

}

你可能感兴趣的:(C# winfrom 中datagridview中checkbox的使用方法)