// Custom control
/ * * Grid list display control * CC 20050420 * / using system; using system.collections; using system.drawing; using system.data; using system.windows.forms;
Namespace WinCommonControl {///
public class CellMouseDownEventArgs: EventArgs {public readonly int row, col; public readonly string cellValue; public CellMouseDownEventArgs (int row, int col, string cellValue) {this.row = row; this.col = col; this.cellValue = cellValue;} }
Public Delegate Void CellmousedowneventHandler (Object Sender, Cellmousedowneventargs E);
Public Event Cellmousedowneventhandler Cellmousedown;
Private system.windows.Forms.DataGrid Dg; ///
Private DataTable_dt; ///
Public DataGridView () {// This call is required for the Windows.Forms Form Designer. InitializationComponent ();
// Todo: Add any initialization after INITIALIZEC Mobomponent call
///
// Grid Displays the binding data initGrid ();
///
} #Endregion
Public void init (DataTable DT, int [] colwidth) {this._dt = dt; this._colwidth = COLWIDTH;
// Grid Displays the binding data initGrid ();
///
//this.dg.dock = DockStyle.Fill; //this.dg.captionvisible = false; this.dg.readonly = true;
// load is DataGrid DataGridTableStyle style if (dg.TableStyles.Contains (this._dt.TableName)!) {DataGridTableStyle dgdtblStyle = new DataGridTableStyle (); dgdtblStyle.MappingName = _dt.TableName; dg.TableStyles.Add (dgdtblStyle); dgdtblStyle .AllowSorting = false; dgdtblStyle.RowHeadersVisible = false; dg.BackgroundColor = Color.White; // set the column width GridColumnStylesCollection colStyle = dg.TableStyles [0] .GridColumnStyles; // not set the default width of each column width is set if ( THIS._DT.COLUMNS.COUNT! = _COLWIDTH.LENGTH) {_COLWIDTH = New int [_dt.columns.count]; for (int i = 0; i <_colwidth.length; i ) _COLWIDTH [i] = 150;}
// Set each column for (int i = 0; i <_d.column.count; i ) {// width colrsele [i] .width = this._colwidth [i];
/ / Do not prohibit the BOOL column value is empty (remove the Checkbox ash status) IF (_dt.columns [i] .DataType == system.type.gettype ("system.boolean") {system.windows.forms.dataGridBoolColumn DTBC; DTBC = (DataGridBoolColumn) Dg.TablesTyles [0] .gridColumnStyles [i]; dtbc.allownull = false;}}}}
private void dg_MouseDown (object sender, System.Windows.Forms.MouseEventArgs e) {DataGrid myGrid = (DataGrid) sender; System.Windows.Forms.DataGrid.HitTestInfo hti; hti = myGrid.HitTest (eX, eY); // string Message = "you click";
Switch (hti.type) {copy system.windows.forms.DataGrid.hittesttype.none: // message = "the background."; break; case system.windows.forms.DataGrid.hittesttype.cell: // message = "Cell at Row" HTI.ROW ", COL" HTI.COLUMN;
/ / If you click the cell of the Bool column, modify the value IF (_dt.columns [hti.column] .DataType == system.type.gettype ("system.boolean") {if (! Bool.parse) MyGrid [hti.row, hti.column] .tostring ())) MyGrid [hti.row, hti.column] = true; else mygrid [hti.row, hti.column] = false;} // cell mousedown event notification if (CellMouseDown = null!) {CellMouseDownEventArgs ee = new CellMouseDownEventArgs (hti.Row, hti.Column, myGrid [hti.Row, hti.Column] .ToString ()); CellMouseDown (sender, ee);} break; case System.Windows.Forms.DataGrid.HitTestType.ColumnHeader: // message = hti.Column "the column header for column"; break; case System.Windows.Forms.DataGrid.HitTestType.RowHeader: // message = " the row header for row " hti.Row; break; case System.Windows.Forms.DataGrid.HitTestType.ColumnResize: // message =" the column resizer for column " hti.Column; break; case System.Windows. Forms.DataGrid.hittesttype.rowresize: // message = "The Row Resizer for Row" HT I. Row; Break; Case System.windows.Forms.DataGrid.hittesttype.caption: // Message = "The CAPTION"; Break; Case System.Windows.Forms.DataGrid.hittesttype.parentrows: // message = " The Parent Row "; Break;} //console.writeline (Message);
} // Class
} // namespace
/ / The following is an example
Private DataTable DT; Private Void Form3_Load (Object Sender, System.EventArgs E) {DT = New DataTable ("DT"); Datacolumn DTCOL = NULL;
// Create a Boolean column and display with CheckedBox. DataColumn dtcCheck = new DataColumn ( ""); dtcCheck.DataType = System.Type.GetType ( "System.Boolean"); dtcCheck.DefaultValue = true; dt.Columns.Add (dtcCheck); // create columns dtCol = new String Datacolumn ("serial number); DTCol.DataType = type.gettype (" system.string "); dtcol.defaultValue =" "; dt.columns.add (dtcol);
// Create a string column DTCOL = New Datacolumn ("Formula"); DTCol.DataType = Type.gettype ("System.String"); DTCOL.DEFAULTVALUE = ""; DT.COLUMNS.ADD (DTCOL);
// Create a Boolean column and display with CheckedBox. DTCCHECK = New Datacolumn ("Auto Verification"); DTCCHECK.DATATYPE = System.Type.gettype ("System.Boolean"); DTCCHECK.DEFAULTVALUE = false; dt.columns.add (dtccheck);
Int [] widthlist = new int [4]; widthlist [0] = 20; widthlist [1] = 30; widthlist [2] = 300; widthlist [3] = 60;
THIS.DATAGRIDVIEW1.INIT (DT, Widthlist);
}
private void AddRow (DataTable myTable) {DataRowCollection rc; DataRow myNewRow; // Create an array with three elements object [] rowVals = new object [4];. rc = myTable.Rows; rowVals [0] = true; rowVals [1 ] = 1; RowVals [2] = ""; RowVals [3] = false; // address and return the new row. Mynewrow = rc.add (rowvals);
Private void button1_click (object sender, system.eventargs e) {addrow (dt);