This article describes how to use the ComboBox control in System.Windows.Forms.DataGrid, which includes three main aspects.
1. Add a ComboBox column to the DataGrid;
2. Save the changes in the DataGrid to the corresponding grid;
3. Sets the focus of the grid in the DataGrid.
Here is the entire source code, some features to see the comments.
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 (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}
Code generated #region the Windows forms Designer
private void InitializeComponent ()
{
This.dgdfunctionarea = 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);
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";
((System.ComponentModel.ISupportInitialize) (This.dgdfunctionarea)). EndInit ();
This. ResumeLayout (FALSE);
}
#endregion
<summary>
The main entry point for the application.
</summary>
[STAThread]
static void Main ()
{
Application.Run (New Form1 ());
}
Initializing DataGrid
private void PopulateGrid ()
{
Creates a DataTable object that includes four columns, the first three as String, and the last column as a Boolean.
Dtblfunctionalarea = new DataTable ("Functionarea");
string[] Arrstrfunctionalarea = new string [3]{"Functional Area", "Min", "Max"};
DataColumn dtcol = null;
Create a 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 to display with Checkedbox.
DataColumn Dtccheck = new DataColumn ("Ismandatory");
Dtccheck.datatype = System.Type.GetType ("System.Boolean");
Dtccheck.defaultvalue = false;
DTBLFUNCTIONALAREA.COLUMNS.ADD (Dtccheck);
Bind a table to a DataGrid
Dgdfunctionarea.datasource = Dtblfunctionalarea;
Load DataGridTableStyle style for DataGrid
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;
To set the width of a column
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 One", "option two", "option Three"});
Cmbfunctionarea.cursor = Cursors.arrow;
Cmbfunctionarea.dropdownstyle= ComboBoxStyle.DropDownList;
Cmbfunctionarea.dock = DockStyle.Fill;
Occurs after the selected item has changed and the change is committed
cmbfunctionarea.selectionchangecommitted + = new EventHandler (cmbfunctionarea_selectionchangecommitted);
Add ComboBox to the first column of DataGridTableStyle
DGTB. TEXTBOX.CONTROLS.ADD (Cmbfunctionarea);
}
Set Focus Simulation
private void GetFocus (int row,int col)
{
First move the focus to the DataGrid.
This.dgdFunctionArea.Focus ();
Move the focus to the DataGridCell
DataGridCell DGC = new DataGridCell (row,col);
This.dgdFunctionArea.CurrentCell = DGC;
DataGridTextBoxColumn DGTB = (datagridtextboxcolumn) dgdfunctionarea.tablestyles[0]. Gridcolumnstyles[col];
Set focus
DGTB. Textbox.focus ();
}
Submit the modified data on the ComboBox to the current grid
private void Cmbfunctionarea_selectionchangecommitted (object sender, EventArgs e)
{
This.dgdfunctionarea[this.dgdfunctionarea.currentcell] = ((ComboBox) sender). Selecteditem.tostring ();
}
Set a new focus
private void Buttonfocus_click (object sender, System.EventArgs e)
{
Focus simulation, set up the third row in the first column
GetFocus (2,0);
}
}
}
Here is the Test interface:
Summarize This is done using the DataGridTextBoxColumn.TextBox.Controls.Add method to add a ComboBox control to the column, and the data is saved with the Combobox.selectionchangecommitted event; set Focus is Implemented by the DataGridTextBoxColumn.TextBox.Focus method. It is also possible to add similar controls such as DateTimePicker in this way.