///1,函数BoolValueChangedEventHandler 和类XXDataGridBoolColumn 主要实现DataGrid,Bool列True和false切换
///避免出现NotNull
#region Adding the new event
public delegate void BoolValueChangedEventHandler(object sender, BoolValueChangedEventArgs e); public class BoolValueChangedEventArgs : EventArgs
{
private int _column;
private int _row;
private bool _value; public BoolValueChangedEventArgs(int row, int col, bool val)
{
_row = row;
_column = col;
_value = val;
} public int Column
{
get{ return _column;}
set{ _column = value;}
}
public int Row
{
get{ return _row;}
set{ _row = value;}
}
public bool BoolValue
{
get{ return _value;}
set{ _value = value;}
}
}
#endregion
#region Derived ColumnStyle with a BoolValueChanged event exposed
public class XXDataGridBoolColumn: DataGridBoolColumn
{
public event BoolValueChangedEventHandler BoolValueChanged;
bool saveValue;
int saveRow ;
bool lockValue;
bool beingEdited;
int _column;
public const int VK_SPACE = 32 ;// 0x20 [System.Runtime.InteropServices.DllImport("user32.dll")]
static extern short GetKeyState(int nVirtKey); public XXDataGridBoolColumn(int column)
{
saveValue = false;
saveRow = -1;
lockValue = false;
beingEdited = false;
_column = column;
} 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);
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 ); // or spacebar
if(!lockValue && beingEdited && changing && saveRow == rowNum)
{
saveValue = !saveValue;
lockValue = false; //fire the event
if(BoolValueChanged != null)
{
BoolValueChangedEventArgs e = new BoolValueChangedEventArgs(rowNum, _column, 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);
}
}
#endregion
***以上是定义列
DataGridTableStyle dgts = new DataGridTableStyle();
dgts.MappingName ="TP_市场订单表"; //自定义表格映射的记录中的表名 DataGridColumnStyle boolCol = new PubLib.XXDataGridBoolColumn(0); //定义bool型列
boolCol.MappingName = "处理标志"; //映射纪录的字段为 :处理标志
boolCol.HeaderText = ""; //列标题
boolCol.Width = 20; //列宽
boolCol.ReadOnly =false; //列的只读书型
((DataGridBoolColumn)boolCol).AllowNull = false; //当列的值为空时默认值
dgts.GridColumnStyles.Add(boolCol);
dgOrder.TableStyles.Clear();
dgOrder.TableStyles.Add(dgts);
///避免出现NotNull
#region Adding the new event
public delegate void BoolValueChangedEventHandler(object sender, BoolValueChangedEventArgs e); public class BoolValueChangedEventArgs : EventArgs
{
private int _column;
private int _row;
private bool _value; public BoolValueChangedEventArgs(int row, int col, bool val)
{
_row = row;
_column = col;
_value = val;
} public int Column
{
get{ return _column;}
set{ _column = value;}
}
public int Row
{
get{ return _row;}
set{ _row = value;}
}
public bool BoolValue
{
get{ return _value;}
set{ _value = value;}
}
}
#endregion
#region Derived ColumnStyle with a BoolValueChanged event exposed
public class XXDataGridBoolColumn: DataGridBoolColumn
{
public event BoolValueChangedEventHandler BoolValueChanged;
bool saveValue;
int saveRow ;
bool lockValue;
bool beingEdited;
int _column;
public const int VK_SPACE = 32 ;// 0x20 [System.Runtime.InteropServices.DllImport("user32.dll")]
static extern short GetKeyState(int nVirtKey); public XXDataGridBoolColumn(int column)
{
saveValue = false;
saveRow = -1;
lockValue = false;
beingEdited = false;
_column = column;
} 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);
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 ); // or spacebar
if(!lockValue && beingEdited && changing && saveRow == rowNum)
{
saveValue = !saveValue;
lockValue = false; //fire the event
if(BoolValueChanged != null)
{
BoolValueChangedEventArgs e = new BoolValueChangedEventArgs(rowNum, _column, 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);
}
}
#endregion
***以上是定义列
DataGridTableStyle dgts = new DataGridTableStyle();
dgts.MappingName ="TP_市场订单表"; //自定义表格映射的记录中的表名 DataGridColumnStyle boolCol = new PubLib.XXDataGridBoolColumn(0); //定义bool型列
boolCol.MappingName = "处理标志"; //映射纪录的字段为 :处理标志
boolCol.HeaderText = ""; //列标题
boolCol.Width = 20; //列宽
boolCol.ReadOnly =false; //列的只读书型
((DataGridBoolColumn)boolCol).AllowNull = false; //当列的值为空时默认值
dgts.GridColumnStyles.Add(boolCol);
dgOrder.TableStyles.Clear();
dgOrder.TableStyles.Add(dgts);
其實很簡單,你在DataTable.ColumnChanged或ColumnChanging事件中處理不就行了嗎。要注意任何時候,DataGrid只是一個顯示ADO.NET數據的容器控件,所有在數據源中的改變,都會實時的映射到表格中去的。