首先新建一个类:DataGridBoolColumnPlus,继承System.Windows.Forms.DataGridBoolColumn
然后定义事件。此事件可以返回当前单元格的内容
然后定义事件。此事件可以返回当前单元格的内容
/**
* UtilityControl.DataGrid 类库
*
* 整理编写:99StAr,Email:sunhuangmin@163.com 电话:13576017263
*
* 最后更新:2005-10-11
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace UtilityControl.DataGrid
{
/// <summary>
/// DataGridBoolColumnPlus 的摘要说明。
/// </summary>
public class DataGridBoolColumnPlus : System.Windows.Forms.DataGridBoolColumn
{
public event BoolValueChangedEventHandler BoolValueChanged;
bool saveValue;
int saveRow ;
bool lockValue;
bool beingEdited;
public const int VK_SPACE = 32 ;// 0x20
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern short GetKeyState(int nVirtKey);
public DataGridBoolColumnPlus()
{
saveValue = false;
saveRow = -1;
lockValue = false;
beingEdited = false;
}
protected override void Edit(System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
{
lockValue = true;
beingEdited = true;
saveRow = rowNum;
saveValue = (bool) base.GetColumnValueAtRow(source, rowNum);
base.Edit(source, rowNum, bounds, readOnly, instantText, cellIsVisible);
}
protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush, bool alignToRight)
{
Point mousePos = this.DataGridTableStyle.DataGrid.PointToClient(Control.MousePosition);
System.Windows.Forms.DataGrid dg = this.DataGridTableStyle.DataGrid;
bool isClickInCell = ((Control.MouseButtons == MouseButtons.Left) &&
dg.GetCellBounds(dg.CurrentCell).Contains(mousePos) );
bool changing = dg.Focused && ( isClickInCell
|| GetKeyState(VK_SPACE) < 0 ); //如果是空格
if(!lockValue && beingEdited && changing && saveRow == rowNum)
{
saveValue = !saveValue;
lockValue = false;
//事件激发
if(BoolValueChanged != null)
{
BoolValueChangedEventArgs e = new BoolValueChangedEventArgs(rowNum, saveValue);
BoolValueChanged(this, e);
}
}
if(saveRow == rowNum)
lockValue = false;
base.Paint( g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
}
protected override bool Commit(System.Windows.Forms.CurrencyManager dataSource, int rowNum)
{
lockValue = true;
beingEdited = false;
return base.Commit(dataSource, rowNum);
}
}
* UtilityControl.DataGrid 类库
*
* 整理编写:99StAr,Email:sunhuangmin@163.com 电话:13576017263
*
* 最后更新:2005-10-11
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace UtilityControl.DataGrid
{
/// <summary>
/// DataGridBoolColumnPlus 的摘要说明。
/// </summary>
public class DataGridBoolColumnPlus : System.Windows.Forms.DataGridBoolColumn
{
public event BoolValueChangedEventHandler BoolValueChanged;
bool saveValue;
int saveRow ;
bool lockValue;
bool beingEdited;
public const int VK_SPACE = 32 ;// 0x20
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern short GetKeyState(int nVirtKey);
public DataGridBoolColumnPlus()
{
saveValue = false;
saveRow = -1;
lockValue = false;
beingEdited = false;
}
protected override void Edit(System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
{
lockValue = true;
beingEdited = true;
saveRow = rowNum;
saveValue = (bool) base.GetColumnValueAtRow(source, rowNum);
base.Edit(source, rowNum, bounds, readOnly, instantText, cellIsVisible);
}
protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush, bool alignToRight)
{
Point mousePos = this.DataGridTableStyle.DataGrid.PointToClient(Control.MousePosition);
System.Windows.Forms.DataGrid dg = this.DataGridTableStyle.DataGrid;
bool isClickInCell = ((Control.MouseButtons == MouseButtons.Left) &&
dg.GetCellBounds(dg.CurrentCell).Contains(mousePos) );
bool changing = dg.Focused && ( isClickInCell
|| GetKeyState(VK_SPACE) < 0 ); //如果是空格
if(!lockValue && beingEdited && changing && saveRow == rowNum)
{
saveValue = !saveValue;
lockValue = false;
//事件激发
if(BoolValueChanged != null)
{
BoolValueChangedEventArgs e = new BoolValueChangedEventArgs(rowNum, saveValue);
BoolValueChanged(this, e);
}
}
if(saveRow == rowNum)
lockValue = false;
base.Paint( g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
}
protected override bool Commit(System.Windows.Forms.CurrencyManager dataSource, int rowNum)
{
lockValue = true;
beingEdited = false;
return base.Commit(dataSource, rowNum);
}
}
/// <summary>
///
/// </summary>
public class BoolValueChangedEventArgs : EventArgs
{
private int _row;
private bool _value;
/// <summary>
///
/// </summary>
/// <param name="row"></param>
/// <param name="val"></param>
public BoolValueChangedEventArgs(int row, bool val)
{
_row = row;
_value = val;
}
/// <summary>
///
/// </summary>
public int Row
{
get{ return _row;}
set{ _row = value;}
}
/// <summary>
///
/// </summary>
public bool BoolValue
{
get{ return _value;}
set{ _value = value;}
}
}
/// <summary>
///
/// </summary>
public delegate void BoolValueChangedEventHandler(object sender, BoolValueChangedEventArgs e);
}
///
/// </summary>
public class BoolValueChangedEventArgs : EventArgs
{
private int _row;
private bool _value;
/// <summary>
///
/// </summary>
/// <param name="row"></param>
/// <param name="val"></param>
public BoolValueChangedEventArgs(int row, bool val)
{
_row = row;
_value = val;
}
/// <summary>
///
/// </summary>
public int Row
{
get{ return _row;}
set{ _row = value;}
}
/// <summary>
///
/// </summary>
public bool BoolValue
{
get{ return _value;}
set{ _value = value;}
}
}
/// <summary>
///
/// </summary>
public delegate void BoolValueChangedEventHandler(object sender, BoolValueChangedEventArgs e);
}