DataGrid column format settings in WinForm

xiaoxiao2021-03-05  22

// 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 {///

/// listView replacement, in a grid, there are multiple columns /// public class database data.userControl {

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; ///

// / / The designer variable. /// private system.componentmodel.container components = null;

Private DataTable_dt; ///

/// Control Binding Data /// Column can only be String or Boolean /// public datatable data {get {return this._dt;} set { THIS._DT = Value;}} /// /// Each column width /// private int [] _COLWIDTH;

Public DataGridView () {// This call is required for the Windows.Forms Form Designer. InitializationComponent ();

// Todo: Add any initialization after INITIALIZEC Mobomponent call

///

/// Initialization, load information /// /// Binding data source /// Columns width public DataGridView (DataTable DT, Int [] ColWidth): this () {this._dt = dt; this._colwidth = COLWIDTH;

// Grid Displays the binding data initGrid ();

///

/// Clean all the resources being used. /// Protected Override Void Dispose (Bool Disposing) {if (disponents! = null) Components.dispose ();} Base.Dispose (Disposing);} #Region Component Designer Generated Code / // /// Designer supports the required method - Do not use the code editor // to modify the contents of this method. /// private () {this.dg = new system.windows.forms.DataGrid (); ("(system.componentmodel.isugg) (this.dg)). Begininit (); this.suspendlayout (); // // DG // this.dg.captionvisible = false; this.dg.datamember = ""; this.dg.dock = system.windows.forms.dockstyle.Fill; this.dg.HeaderForeColor = system .Drawing.systemcolors.ControlText; this.dg.name = "DG"; this.dg.size = new system.drawing.size (150, 150); this.dg.tabindex = 0; this.dg.mousedown = new System.Windows.Forms.MouseEventHandler (this.dg_MouseDown); // // DataGridView // this.Controls.AddRange (new System.Windows.Forms.Control [] {this.dg}); this.Name = "DataGridView "(System.comPonentModel.isupportInitialize (this.dg)). Endinit (); this.ResumeLayout (false);

} #Endregion

Public void init (DataTable DT, int [] colwidth) {this._dt = dt; this._colwidth = COLWIDTH;

// Grid Displays the binding data initGrid ();

///

/// Grid load /// private void initGRID () {// Binds the table to DataGrid this.dg.dataSource = this._dt;

//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);

转载请注明原文地址:https://www.9cbs.com/read-33155.html

New Post(0)