Add a drop-down list box for DataGrid in C #

xiaoxiao2021-03-06  109

This article will show you how to use ComboBox in System.Windows.Forms.DataGrid

Controls, mainly including three aspects.

1. Add a ComboBoX column in DataGrid;

2. Save the modifications in the DataGrid to the corresponding grid; Set the focus of the grid in the DataGrid.

Below is the entire source code, some features can be commented.

Using system.drawing; using system.componentmodel; using system.windows.form; using system.data ;. using system.data ;. USING SYSTEM.DATA;

namespace DataGridTest {public class Form1: System.Windows.Forms.Form {private System.Windows.Forms.DataGrid dgdFunctionArea; private DataTable dtblFunctionalArea; private System.Windows.Forms.Button buttonFocus; private System.ComponentModel.Container components = null;

Public form1 () {initializecomponent (); populategrid ();

Protected Override Void Dispose (bool disposing) {if (disponents! = null) {components.dispose ();}} Base.Dispose (Disposing);

#Region Windows Form Designer Generated Code

private void InitializeComponent () {this.dgdFunctionArea = new System.Windows.Forms.DataGrid (); this.buttonFocus = new System.Windows.Forms.Button (); ((System.ComponentModel.ISupportInitialize) (this.dgdFunctionArea)) .Beininit (); this.suspendlayout (); // // DGDFunctionArea // this.dgdfunctionarea.datamember = ""; this.dgdfunctionarea.headerforeColor = system.drawing.systemcolors.ControlText;

this.dgdFunctionArea.Location = new System.Drawing.Point (4, 8); this.dgdFunctionArea.Name = "dgdFunctionArea"; this.dgdFunctionArea.Size = new System.Drawing.Size (316, 168); this.dgdFunctionArea. TabIndex = 0; // // buttonFocus // this.buttonFocus.Location = new System.Drawing.Point (232, 188); this.buttonFocus.Name = "buttonFocus"; this.buttonFocus.Size = new System.Drawing. Size (84, 23); this.buttonfocus.tabindex = 1; this.buttonfocus.text = "Get Focus"; this.buttonfocus.click = new system.eventhandler (this.buttonfocus_click; // // Form1 // THIS.AUTOSCALEBASESIZE = New System.drawing.Size (6, 14); this.clientsize = new system.drawing.size (332, 217); this.controls.add (this.buttonfocus); this.controls.add (this .dgdfunctionarea); this.name = "form1"; this.text = "form1"; ("this.dgdfunctionArea). endinit (); this.ResumeLayout (false);

} #ENDREGION ///

/// Application of primary invested points. /// [stathread] static void main () {Application.Run (new form1 ());} // Initialization DataGrid Private Void PopulateGrid () {// Create a DataTable object, including four columns, the first three Listing as String, the last list is Boolean. DTBLFunctionAlarea = New DataTable ("functionarea"); string [] arrstrfunctionAlarea = new string [3] {"functional area", "min", "max"}; Datacolumn DTCOL = null; // Create String column for (int i = 0; i <3; i ) {dtCol = new DataColumn (arrstrFunctionalArea [i]); dtCol.DataType = Type.GetType ( "System.String"); dtCol.DefaultValue = ""; dtblFunctionalArea.Columns.Add (dtCol) ;} // Create a boolean column, display with check -dbox. DataColumn dtcCheck = new DataColumn ( "IsMandatory"); dtcCheck.DataType = System.Type.GetType ( "System.Boolean"); dtcCheck.DefaultValue = false; dtblFunctionalArea.Columns.Add (dtcCheck);

// Bind the table to DataGrid DGDFunctionArea.DataSource = DTBLFunctionAlandA;

// load is DataGrid DataGridTableStyle style if (dgdFunctionArea.TableStyles.Contains ( "FunctionArea")!) {DataGridTableStyle dgdtblStyle = new DataGridTableStyle (); dgdtblStyle.MappingName = dtblFunctionalArea.TableName; dgdFunctionArea.TableStyles.Add (dgdtblStyle); dgdtblStyle.RowHeadersVisible = false; dgdtblStyle.HeaderBackColor = Color.LightSteelBlue; dgdtblStyle.AllowSorting = false; dgdtblStyle.HeaderBackColor = Color.FromArgb (8,36,107); dgdtblStyle.RowHeadersVisible = false; dgdtblStyle.HeaderForeColor = Color.White; dgdtblStyle.HeaderFont = new System .Drawing.Font ( "Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte) (0))); dgdtblStyle.GridLineColor = Color.DarkGray; dgdtblStyle .PreferredRowHeight = 22; dgdFunctionArea.BackgroundColor = Color.White; // set the column width GridColumnStylesCollection colStyle = dgdFunctionArea.TableStyles [0] .GridColumnStyles; colStyle [0] .Width = 100; colStyle [ 1] .width = 50; colStyle [2] .width = 50; colStyle [3] .width = 80;

DataGridTextBoxColumn DGTB = (DataGridTextBoxColumn) DGDFunctionArea.tableStyles [0] .gridcolumnStyles [0];

ComboBox cmbFunctionArea = new ComboBox (); cmbFunctionArea.Items.AddRange (new object [] { "Option 1", "Option 2", "Option 3"}); cmbFunctionArea.Cursor = Cursors.Arrow; cmbFunctionArea.DropDownStyle = ComboBoxStyle. DropDownList; cmbfunctionarea.dock = dockstyle.fill;

/ / When the selected item changes and the changes are submitted

CMBFunctionArea.selectionChangeCommitted = New EventHandler (CMBFunctionArea_selection);

// Add ComboBox to the first column of DataGridTableStyle

DGTB.TextBox.controls.add (cMBFunctionarea);

// Set the focus simulation

private void GetFocus (int row, int col) {// move the focus to the first DataGrid this.dgdFunctionArea.Focus (); // moves the focus to DataGridCell DataGridCell dgc = new DataGridCell (row, col); this.dgdFunctionArea.CurrentCell = DGC; DataGridTextBoxColumn DGTB = (DataGridTextBoxColumn) DGDFunctionArea.tableStyles [0] .gridcolumnStyles [col];

// Setting the focus

DGTB.TextBox.focus ();

// Submit the data modified on ComboBox to the current grid

Private void cmbfunctionarea_selectionchangecommitted (Object sender, eventargs e) {this.dgdfunctionarea [this.dgdfunctionarea.currentcell] = ((comboBox) sender .SelectedItem.toString ();

}

/ / Set new focus

Private void buttonfocus_click (object sender, system.eventargs e) {// focus simulation, set the third line of the first column getFocus (2,0);}}

}

Below is the test interface:

Summary, here is to add a ComboBox control in the column through the DataGridTextBoxColumn.TextBox.controls.Add method; for the saving of the data is done using the ComboBox.SelectionChangeCombMitted event; setting the focus is implemented through the DataGridTextBoxColumn.TextBox.focus method. In addition, similar controls such as DateTimePicker can be added to this method.

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

New Post(0)