先看效果吧,第一张图片是全选的时候获取到“随机数值”列的结果
下面这张是选择了部分行获取的选中行的第二列的结果
下面来说说他的实现过程。我主要还是参照
http://www.cnblogs.com/wuhuacong/archive/2011/12/30/2307600.html
实现的。
首先你要确保你安装了DotNetBar控件,如果没有安装就要到http://www.devcomponents.com/dotnetbar/ 网站下载安装winform版本的即可。
安装完毕之后,打开vs就可以看到相应的控件
如果没有出现对应的控件,则需要手工添加控件即可。
然后就会新建windows窗体应用程序,在工具箱中找到DataGridViewX 控件,拖动到窗体上,然后添加列并设置属性:
需要特别注意的是需要填写属性“Name”、“DataPropertyName”、“ColumnType”。这三个属性分别对应着列的名称、绑定的数据列、列的类型。当然我们还需要设置datagridview的其他属性,如列标题居中,自适应窗体等属性。
接下类我们就是要添加checkbox列,其实也就是第一列,主要是改变列的显示方式。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : DevComponents.DotNetBar.Office2007RibbonForm
{
CheckBox HeaderCheckBox = null;
public Form1()
{
InitializeComponent();
if (!this.DesignMode)
{
HeaderCheckBox = new CheckBox();
HeaderCheckBox.Size = new Size(15, 15);
this.dataGridViewX1.Controls.Add(HeaderCheckBox);
HeaderCheckBox.KeyUp += new KeyEventHandler(HeaderCheckBox_KeyUp);
HeaderCheckBox.MouseClick += new MouseEventHandler(HeaderCheckBox_MouseClick);
dataGridViewX1.CurrentCellDirtyStateChanged += new EventHandler(dgvSelectAll_CurrentCellDirtyStateChanged);
dataGridViewX1.CellPainting += new DataGridViewCellPaintingEventHandler(dgvSelectAll_CellPainting);
}
}
private void Form1_Load(object sender, EventArgs e)
{
BindGridView();
}
private void BindGridView()
{
dataGridViewX1.DataSource = GetDataSource();
}
private List GetDataSource()
{
List dt = new List();
DateTime dTime;
for (int i = 0; i < 20; i++)
{
dTime = DateTime.Now;
xaut temp = new xaut();
temp.IsChecked = false;
temp.RandomNo = i;
temp.Date = dTime.ToString("yyyy-MM-dd");
temp.Time = dTime.ToString("19:mm:ss tt");
dt.Add(temp);
}
return dt;
}
private void HeaderCheckBox_MouseClick(object sender, MouseEventArgs e)
{
HeaderCheckBoxClick((CheckBox)sender);
}
private void dgvSelectAll_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dataGridViewX1.CurrentCell is DataGridViewCheckBoxCell)
dataGridViewX1.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
private void dgvSelectAll_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex == -1 && e.ColumnIndex == 0)
ResetHeaderCheckBoxLocation(e.ColumnIndex, e.RowIndex);
}
private void ResetHeaderCheckBoxLocation(int ColumnIndex, int RowIndex)
{
Rectangle oRectangle = this.dataGridViewX1.GetCellDisplayRectangle(ColumnIndex, RowIndex, true);
Point oPoint = new Point();
oPoint.X = oRectangle.Location.X + (oRectangle.Width - HeaderCheckBox.Width) / 2 + 1;
oPoint.Y = oRectangle.Location.Y + (oRectangle.Height - HeaderCheckBox.Height) / 2 + 1;
HeaderCheckBox.Location = oPoint;
}
private void HeaderCheckBoxClick(CheckBox HCheckBox)
{
foreach (DataGridViewRow Row in dataGridViewX1.Rows)
{
((DevComponents.DotNetBar.Controls.DataGridViewCheckBoxXCell)Row.Cells["chkBxSelect"]).Value = HCheckBox.Checked;
}
dataGridViewX1.RefreshEdit();
}
private void HeaderCheckBox_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
HeaderCheckBoxClick((CheckBox)sender);
}
private void buttonX1_Click(object sender, EventArgs e)
{
//获取选中的行
string temp = "";
foreach (DataGridViewRow row in dataGridViewX1.Rows)
{
if ((bool)((DevComponents.DotNetBar.Controls.DataGridViewCheckBoxXCell)row.Cells["chkBxSelect"]).Value)
{
temp += row.Cells["RandomNo"].Value + "_";
}
}
MessageBox.Show(temp);
}
}
}
在上面的红色标注部分是针对dotnetbat的,对于winform 中控件datagridview而言就不一样了。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication1
{
public class xaut
{
public bool IsChecked { get; set; }
public int RandomNo { get; set; }
public string Date { get; set; }
public string Time{get;set;}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WHC.OrderWater.Commons;
namespace TestGridViewCheck
{
public partial class FrmNormalGridViewSelect : Form
{
CheckBox HeaderCheckBox = null;
public FrmNormalGridViewSelect()
{
InitializeComponent();
if (!this.DesignMode)
{
HeaderCheckBox = new CheckBox();
HeaderCheckBox.Size = new Size(15, 15);
this.dgvSelectAll.Controls.Add(HeaderCheckBox);
HeaderCheckBox.KeyUp += new KeyEventHandler(HeaderCheckBox_KeyUp);
HeaderCheckBox.MouseClick += new MouseEventHandler(HeaderCheckBox_MouseClick);
dgvSelectAll.CurrentCellDirtyStateChanged += new EventHandler(dgvSelectAll_CurrentCellDirtyStateChanged);
dgvSelectAll.CellPainting += new DataGridViewCellPaintingEventHandler(dgvSelectAll_CellPainting);
}
}
private void frmSelectAll_Load(object sender, EventArgs e)
{
BindGridView();
}
private void BindGridView()
{
dgvSelectAll.DataSource = GetDataSource();
}
private DataTable GetDataSource()
{
string columnsList = "IsChecked|bool,RandomNo,Date,Time";
DataTable dt = DataTableHelper.CreateTable(columnsList);
DataRow dr = null;
DateTime dTime;
Random rnd = new Random();
for (int i = 0; i < 100; i++)
{
dr = dt.NewRow();
dTime = DateTime.Now;
dr["IsChecked"] = "false";
dr["RandomNo"] = rnd.NextDouble();
dr["Date"] = dTime.ToString("yyyy-MM-dd");
dr["Time"] = dTime.ToString("19:mm:ss tt");
dt.Rows.Add(dr);
}
return dt;
}
private void dgvSelectAll_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (dgvSelectAll.CurrentCell is DataGridViewCheckBoxCell)
dgvSelectAll.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
private void HeaderCheckBox_KeyUp(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Space)
HeaderCheckBoxClick((CheckBox)sender);
}
private void HeaderCheckBox_MouseClick(object sender, MouseEventArgs e)
{
HeaderCheckBoxClick((CheckBox)sender);
}
private void dgvSelectAll_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (e.RowIndex == -1 && e.ColumnIndex == 0)
ResetHeaderCheckBoxLocation(e.ColumnIndex, e.RowIndex);
}
private void ResetHeaderCheckBoxLocation(int ColumnIndex, int RowIndex)
{
Rectangle oRectangle = this.dgvSelectAll.GetCellDisplayRectangle(ColumnIndex, RowIndex, true);
Point oPoint = new Point();
oPoint.X = oRectangle.Location.X + (oRectangle.Width - HeaderCheckBox.Width) / 2 + 1;
oPoint.Y = oRectangle.Location.Y + (oRectangle.Height - HeaderCheckBox.Height) / 2 + 1;
HeaderCheckBox.Location = oPoint;
}
private void HeaderCheckBoxClick(CheckBox HCheckBox)
{
foreach (DataGridViewRow Row in dgvSelectAll.Rows)
{
((DataGridViewCheckBoxCell)Row.Cells["chkBxSelect"]).Value = HCheckBox.Checked;
}
dgvSelectAll.RefreshEdit();
}
}
}
在上述代码中,我们只需要把数据源部分改变成list<>即可。
源码下载