Simply use the drop-down list box in DataGrid

xiaoxiao2021-03-05  21

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 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.

///

[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 the 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.

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.

[

Click here to favor this article]

Published on May 31, 2004 4:50 PM

href = "http://blog.9cbs.net/zhzuo/services/pingback.aspx" Rel = "pingback" />

Jacky published

2004-07-28 1:33 AM

If the added COMBO data source is dynamic?

For example, the option content of Combo in Functional Area needs to take the corresponding value from the database according to the value of MIN.

Qiu Feng published

2004-07-28 2:51 PM

Use the ComboBox.Dropdown event to complete the work,

For the ComboBoX control registration Dropdown event, the above example is example,

CMBFunctionArea.dropdown = New EventHandler (CMBFunctionArea_Dropdown);

// The event handler is as follows:

Private Void CMBFunctionArea_Dropdown (Object Sender, Eventargs E)

{

/ /> Written data from the database or memory data set or other data source according to the value of other columns,

/ / But it is necessary to pay attention to the case of relying on the columns, there is no data.

// The following is that I simulates the use of ArrayList to make the container to get data. ComboBox Box = (ComboBox) Sender;

//box.datasource = null;

//box.Items.clear ();

ArrayList Al = New ArrayList ();

//retrieve data

Random r = new random ();

Al.Add (R.Next (1,1000));

Al.Add (R.Next (1,1000));

Al.Add (R.Next (1,1000));

Al.Add (R.Next (1,1000));

Al.Add (R.Next (1,1000));

Box.DataSource = Al;

/ / The above is bound, or you can fill it directly into the Box item.

}

Clamphammer published

2004-07-28 3:01 PM

There is a problem, I bind ComboBox to a data source, I have run your example

Yes, but I have a standard, his primary key is to choose in the drop-down box, but the value in ComboBox is added.

No correspondence with the value inside the binding manager CurrencyManager (in fact, the ComboBox of the new row is over the binding manager)

Range, I don't know how to update this new line to the data source.

Qiu Feng published

2004-07-28 3:18 PM

If DataGrid is bound to DataView,

I can add some code in the above function as follows:

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

/ / Submit the current changes

This.bindingContext [this.dgdfunctionarea.datasource, this.dgdfunctionarea.datamember] .endcurrented ();

}

/ / Here ComboBox actually plays the role of selecting data and does not implement other implementations.

Jacky published

2004-07-28 8:06 PM

So how do you get the contents of MIN columns in MBFunctionArea_DropDown?

Qiu Feng published

2004-07-30 8:35 AM

// The required relevant line of data displayed below,

Private Void CMBFunctionArea_Dropdown (Object Sender, Eventargs E)

{

/ / Display the relevant column data

Messagebox.show (this.dgdfunctionarea [this.dgdfunctionarea.currentrowindex, 1] .tostring ());

/ /> Written data from the database or memory data set or other data source according to the value of other columns,

/ / But it is necessary to pay attention to the case of relying on the columns, there is no data.

// The following is that I simulates the use of ArrayList to make the container to get data.

ComboBox Box = (ComboBox) Sender;

//box.datasource = null;

//box.Items.clear ();

ArrayList Al = New ArrayList ();

//retrieve data

Random r = new random ();

Al.Add (R.Next (1,1000));

Al.Add (R.Next (1,1000));

Al.Add (R.Next (1,100)); Al.Add (R.Next (1,100));

Al.Add (R.Next (1,1000));

Box.DataSource = Al;

/ / The above is bound, or you can fill it directly into the Box item.

}

BTUT2004 published in

2004-08-31 10:39 AM

DGCOLUMN = New DataGridTextBoxColumn (); // Build a new column

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.selection = new eventhandler (cMBFunctionArea_selection);

// Add ComboBox to the first column of DataGridTableStyle

DGColumn.TextBox.controls.add (cmbfunctionarea); // Add controls to the column

DGTSTYLE.GRIDCOLUMNSTYLES.ADD (DGCOLUMN); // Add column settings to the style sheet

Why can't this such code don't have this column, but it will not be wrong ??????

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

New Post(0)