Winform DataGridView添加列头checkbox
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using Yanwen.Logistics.Business.Logics;
using Yanwen.Logistics.Business.Models.ExpressTransport;
using Yanwen.Logistics.Desktop.Extensions; namespace Yanwen.Logistics.Desktop.Transport
{
public partial class TransportLanshouForm : Form
{
public TransportLanshouForm()
{
InitializeComponent();
}
//显示行号
private void gdvData_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
var b = new SolidBrush(gdvData.RowHeadersDefaultCellStyle.ForeColor);
e.Graphics.DrawString((e.RowIndex + ).ToString(System.Globalization.CultureInfo.CurrentUICulture), gdvData.DefaultCellStyle.Font, b, e.RowBounds.Location.X + , e.RowBounds.Location.Y + );
}
private void TransportLanshouForm_Load(object sender, EventArgs e)
{
var ch = new DatagridviewCheckboxHeaderCell();
ch.OnCheckBoxClicked += new DatagridviewcheckboxHeaderEventHander(ch_OnCheckBoxClicked);//关联单击事件
var checkboxCol = this.gdvData.Columns[] as DataGridViewCheckBoxColumn;
checkboxCol.HeaderCell = ch;
checkboxCol.HeaderCell.Value = string.Empty;//消除列头checkbox旁出现的文字
} /// <summary>
/// 单击事件
/// </summary>
private void ch_OnCheckBoxClicked(object sender, DatagridviewCheckboxHeaderEventArgs e)
{
foreach (DataGridViewRow dgvRow in this.gdvData.Rows)
{
dgvRow.Cells[].Value = e.CheckedState;
}
}
} //定义触发单击事件的委托
public delegate void DatagridviewcheckboxHeaderEventHander(object sender, DatagridviewCheckboxHeaderEventArgs e); //定义包含列头checkbox选择状态的参数类
public class DatagridviewCheckboxHeaderEventArgs : EventArgs
{
public DatagridviewCheckboxHeaderEventArgs()
{
CheckedState = false;
} public bool CheckedState { get; set; }
} //定义继承于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 / ) - (s.Width / ) - ;//列头checkbox的X坐标
p.Y = cellBounds.Location.Y +
(cellBounds.Height / ) - (s.Height / );//列头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)
{ var 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的选择状态
var ex = new DatagridviewCheckboxHeaderEventArgs { CheckedState = _checked }; var sender = new object();//此处不代表选择的列头checkbox,只是作为参数传递。应该列头checkbox是绘制出来的,无法获得它的实例
if (OnCheckBoxClicked != null)
{
OnCheckBoxClicked(sender, ex);//触发单击事件
this.DataGridView.InvalidateCell(this);
}
}
base.OnMouseClick(e);
} } }
Winform DataGridView添加列头checkbox的更多相关文章
- [DevExpress]GridControl 同步列头checkbox与列中checkbox状态
关键代码: /// <summary> /// 同步列头checkbox与列中checkbox状态 /// </summary> /// <param name=&quo ...
- 设置DatagridView的列头样式
设置DataGridView.ColumnHeaderDefaultCellStyle的BackColor属性会发现没有效果.这是因为在启动了可视样式的时候,BackColor和ForeColor的值 ...
- winform DataGridView添加合计行
使用方法 /* DataTable dt= DBUtility.DB.FromSql(sql).ToDataTable(); DataGridViewAddSumRow sumRow = new Da ...
- [WinForm]DataGridView列头右键菜单
[WinForm]DataGridView列头右键菜单 前言 继续"不误正业" - - #,记录一下.有时候有这样的需求:DataGridView的列头菜单可以选择具体显示哪些列, ...
- C#中DataGridView动态添加行及添加列的方法
http://www.jb51.net/article/72259.htm Datagridview添加列: ? 1 2 3 4 5 DataGridViewTextBoxColumn acCode ...
- .NET组件控件实例编程系列——5.DataGridView数值列和日期列
在使用DataGridView编辑数据的时候,编辑的单元格一般会显示为文本框,逻辑值和图片会自动显示对应类型的列.当然我们自己可以手工选择列的类型,例如ComboBox列.Button列.Link列. ...
- DataGridView数值列和日期列
本文转自:http://www.cnblogs.com/conexpress/p/5923324.html 在使用DataGridView编辑数据的时候,编辑的单元格一般会显示为文本框,逻辑值和图片会 ...
- C++ 简单实现MFC ListControl 点击列头排序
说明: SetItemData可以为每一行绑定一个DWORD类型的变量.用GetItemData可以获得这个变量.举个例子,假设CListCtrl中你需要显示某个数据表中的记录,该表有个流水号主键ID ...
- winform datagridview在添加全选checkbox时提示:不能设置 selected 或 selected 既不是表 Table 的 DataColumn 也不是 DataRelation。
在项目中,需要多选功能,于是在datagridview添加了一列DataGridViewCheckBoxColumn 在给datagridview绑定完数据集之后,对全选进行操作的时候,发现总报错,报 ...
随机推荐
- c#_表单处理方式
阅读目录 开始 简单的表单,简单的处理方式 表单提交,成功控件 多提交按钮的表单 上传文件的表单 MVC Controller中多个自定义类型的传入参数 F5刷新问题并不是WebForms的错 以Aj ...
- bash的for循环从命令读取值
bash的for循环可以很方便地从命令读取值,还可以指定分割值 下面的程序可以打印文件的内容,前面加上行号 #!/bin/bash # 打印每一行的内容,前面加行号 filename="/h ...
- hdu 3849 (双联通求桥)
一道简单的双联通求桥的题目,,数据时字符串,,map用的不熟练啊,,,,,,,,,,,,, #include <iostream> #include <cstring> #in ...
- json对象,使用 “ . ”获取值是,不能使用变量作为属性名。
var he={'aa':"aa",'bb':'bb'}; var chun={'cc':"aa",'dd':'mm'}; c=he.aa; n=chun.c; ...
- 在 CentOS 中编译安装 VIM 7.3
转载:http://blog.csdn.net/zhanglyung/article/details/6204574 默认安装的 Vim 不带有多字符支持,所以不支持中文.无论是将 CentOS 本来 ...
- UTF8 与 UTF16 编码
Unicode 的发展,英文好的直接去 unicode.org 上去看吧,不好的可以移步到这里 看dengyunze的总结:<关于UTF8,UTF16,UTF32,UTF16-LE,UTF16- ...
- css笔记11:选择器练习
1. (1)exam1.css文件: .s1 { font-size: 50px; color: blue; } .s2 { backgoround:gray; font-style: italic; ...
- C++编译器的函数名修饰规则
我们知道在C++中有函数重载这样一个东西,当我们定义了几个功能类似且函数名是一样的函数的时候,只要它的参数列表不同,编译是可以通过的,但是在C中是不可以的. double add(double a, ...
- linux云计算集群架构学习笔记:rhel7基本命令操作
1-3-RHEL7基本命令操作 1.1Linux终端介绍 Shell提示符 Bash Shell基本语法. 1.2基本命令的使用:ls.pwd.cd. 1.3查看系统和BIOS硬件时间. 1.4 L ...
- 基于BaseHTTPServer的简单存储服务器
服务器代码: from BaseHTTPServer import BaseHTTPRequestHandler from BaseHTTPServer import HTTPServer impor ...