1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
| #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 MonDataGridBoolColumn: DataGridBoolColumn
{
public event BoolValueChangedEventHandler BoolValueChanged;
bool saveValue;
int saveRow ;
bool lockValue;
bool beingEdited;
int _column;
public const int VK_SPACE = 32 ;// 0x20
public const int VK_ENTREE = 13;
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern short GetKeyState(int nVirtKey);
public MonDataGridBoolColumn(int column)
{
saveValue = false;
saveRow = -1;
lockValue = false;
_column = column;
//base.AllowNull=false;
//base.NullValue=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;
try
{
if(GetColumnValueAtRow(source, rowNum).ToString()=="")
{
saveValue=true;
}
else
{
//string test =GetColumnValueAtRow(source, rowNum).ToString
saveValue = (bool) base.GetColumnValueAtRow(source, rowNum);
}
}
catch
{
saveValue=false;
}
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 || GetKeyState(VK_ENTREE)<0); // or spacebar
if(!lockValue && 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
#region Handler for catching the bool changes
private void HandleBoolChanges(object sender, BoolValueChangedEventArgs e)
{
string s = string.Format("Bool Changes: row {0}, col {1} value {2}", e.Row, e.Column, e.BoolValue);
Console.WriteLine(s);
if (e.BoolValue==true)
{
btnsupp.Enabled = true;
}
else
{
btnsupp.Enabled = false;
for(int i=0;i<ds_grossiste.Tables[0].Rows.Count;i++)
{
if(Convert.ToBoolean(dgAvoirs[i,5])==true)
{
btnsupp.Enabled =true;
goto Fin;
}
}
}
Fin:
;
}
#endregion |