Simply use the drop-down list box in DataGrid
Author: Tushar Ameta
Translation: Zheng Zo
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 the 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;
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 DTBLFUNCTIONAAAAA;
Private system.windows.Forms.Button ButtonFocus;
Private system.componentmodel.Container Components = NULL;
Public Form1 ()
{
InitializationComponent ();
Populategrid ();
}
Protected Override Void Dispose (Bool Disposing)
{
IF (Disposing)
{
IF (Components! = NULL)
{
Components.dispose ();
}
}
Base.dispose (Disposing);
}
#Region Windows Form Designer Generated Code
Private vidinitiRizeComponent ()
{
THIS.DGDFunctionArea = new system.windows.forms.dataGrid ();
This.Buttonfocus = new system.windows.forms.button ();
(System.comPonentModel.isupportInitialize) (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) (DGDFunctionArea). Endinit ();
This.ResumeLayout (false);
}
#ndregion
///
/// The main entry point for the application.
/// summary>
[Stathread]
Static void main ()
{
Application.run (New Form1 ());
}
// Initialize DataGrid
Private void populategrid ()
{
// Create a DataTable object, including four columns, the first three columns as String, the last list as 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 and display with CheckedBox.
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 = DTBLFunctionAlarea; / / Load DataGridTableStyle style for DataGrid
IF (! DGDFunctionArea.tables.contains ("Functionarea"))
{
DataGridTableStyle DGDTBLStyle = New DataGridTableStyle ();
DGDTBLStyle.mappingName = DTBLFunctionAlarea.tablename;
DGDFunctionArea.tables.Add (DGDTBLStyle);
DGDTBLSty.Rowheadersvisible = false;
DGDTBLStyle.HeaderbackColor = Color.lightsteelBlue;
DGDTBLStyle.Allowsorting = false;
DGDTBLStyle.HeaderbackColor = Color.Fromargb (8, 36, 107);
DGDTBLSty.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 width of the column
GridColumnStylesCollection ColStyle = DGDFunctionArea.tables [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 [] {"Options 1", "Option 2", "Options 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 co)
{
// move the focus to DataGrid
THIS.DGDFunctionArea.focus ();
/ / Move the focus to DataGridcell
DataGridcell DGC = New DataGridcell (Row, Col);
this.dgdfunctionarea.currentcell = DGC;
DataGridTextBoxColumn DGTB = (DataGridTextBoxColumn) DGDFunctionArea.tables [0] .gridcolumnStyles [col];
// Setting the focus
DGTB.Textbox.focus ();
}
// Submit the data modified on ComboBox to the current grid
Private Void CMBFunctionArea_SelectionChangeCombMitted (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 first column of the third line here
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.