Simply use the drop-down list box in DataGrid
Author: Tushar Ameta
Translation: Qiu Feng
Use the drop-down list problem in DataGrid. This article tells how to use the ComboBox control in System.Windows.Forms.DataGrid. However, the original text is incomplete, and it is implicit. In order to clear the point here, some modifications have been made, and the entire article mainly includes three aspects.
1. Add a ComboBoX column in DataGrid;
2. Save the modifications in the DataGrid to the corresponding grid;
3. Set the focus of the grid in the DataGrid.
Below is the entire source code, some features can be commented.
Using system;
using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; 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 (disposing) {if (component! = null) {components.dispose ();}} Base.Dispose (4);} #Region Windows Form Designer Generated Code Void InitializationComponent () {this.dgdfuncti onArea = new System.Windows.Forms.DataGrid (); this.buttonFocus = new System.Windows.Forms.Button ();. ((System.ComponentModel.ISupportInitialize) (this.dgdFunctionArea)) BeginInit (); this.SuspendLayout (); // // dgdFunctionArea // this.dgdFunctionArea.DataMember = ""; this.dgdFunctionArea.HeaderForeColor = System.Drawing.SystemColors.ControlText; this.dgdFunctionArea.Location = new System.Drawing.Point (4, 8) Id.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); } #Ndregion ///
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); // table to bind to DataGrid dgdFunctionArea.DataSource = dtblFunctionalArea; // if the style is loaded DataGridTableStyle DataGrid (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; GridColumnStylesCollection colStyle // set the width of a column = 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; // occurs when the selected item changes occurred and committed this change cmbFunctionArea.SelectionChangeCommitted = new EventHandler (cmbFunctionArea_SelectionChangeCommitted); // add the ComboBox to the first DataGridTableStyle A column of DGTB.TextBox.Controls.add (cMBFunctionarea);} // Setting the focus simulation private void getfocus (int → int co) {// first move the focus to DAT aGrid 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]; // set the focus dgtb.TextBox.Focus ();} // modify the submitted data to the current Combobox grid private void cmbFunctionArea_SelectionChangeCommitted (object sender, EventArgs e) {this.dgdFunctionArea [this.dgdFunctionArea.CurrentCell ] =
(ComboBox) .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);}}} The following 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.