.Net DataGrid combobox Simple Sample

ASP DataGridColumns .NET assembly
More about ASPDataGridColumns.dll
Download ASPDataGridColumns.dll
Order ASPDataGridColumns.dll

The sample introduces how to add the Combobox DataGrid Column Style into a DataGrid on your .NET Windows form.
The main idea of the sample is to provide you with a suggestion how to update values in other datagrid cells at a row when the current combobox cell has been updated. For this case when we need to update corresponding cells we are using the DataGrid ComboBoxColumn Leave event. When the user will leave changed combobox cell the event will have effect. In this example project when you change a value in Country column a suitable Capital name will be populated into Capital column and vise versa.

This combobox column style is not just a dropdown combobox, which appears when a datagrid cell becomes the current cell. The combobox DataGridColumnStyle automatically fills the text at the cursor with the value of its dropdown values list. As characters are typed, the component finds the closest matching item from the list and fills in the remaining characters.

The Simple Sample describes the Datagrid Combobox basic techniques only. To find out other attractive Datagrid Combobox Column’s features please learn our Datagrid Columns sample projects. Also the code example gives hints how to deal with a database when you need update a set of records in the .NET datagrid interface and store the updates back into datasource database.

Download the Datagrid Combobox Simple Sample for VB.NET and C# that show how you can use the RustemSoft .NET DataGrid Combobox Column in Windows Forms.

VB.NET

' Set datagrid column styles
' Define comboCountry variable as a ComboBoxColumn column style object of DataGridComboBoxColumn class
Private WithEvents comboCountry As DataGridComboBoxColumn
' Define comboCapital variable as a ComboBoxColumn column style object of DataGridComboBoxColumn class
Private WithEvents comboCapital As DataGridComboBoxColumn

...........

' Set column styles
With .GridColumnStyles
' Set datagrid ComboBox Column Style for Country field
comboCountry = New DataGridComboBoxColumn(Dictionary, 0, 0, , , False)
.Add (comboCountry)
With .Item(0)
.MappingName = "Country"
.HeaderText = "Country"
.Width = 165
.NullText = String.Empty
End With

' Set datagrid ComboBox Column Style for Capital field
comboCapital = New DataGridComboBoxColumn(Dictionary, 1, 1, , , False)
.Add (comboCapital)
With .Item(1)
.MappingName = "Capital"
.HeaderText = "Capital"
.Width = 165
.NullText = String.Empty
End With

End With

...........

' "Country" datagrid cell has been left. Update corresponding "Capital" cell value in the current datagrid row
Private Sub comboCountry_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles comboCountry.Leave
Dim Capital As DataRow() = Dictionary.Select("Country='" + comboCountry.combo.Text + "'")
DataGrid1.Item(DataGrid1.CurrentRowIndex, 1) = Capital(0)(1)
End Sub

' "Capital" datagrid cell has been left. Update corresponding "Country" cell value in the current datagrid row
Private Sub comboCapital_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles comboCapital.Leave
Dim Country As DataRow() = Dictionary.Select("Capital='" + comboCapital.combo.Text + "'")
DataGrid1.Item(DataGrid1.CurrentRowIndex, 0) = Country(0)(0)
End Sub

C#

// Set column styles
// Define comboCountry variable as a ComboBoxColumn column style object of DataGridComboBoxColumn class
internal DataGridComboBoxColumn comboCountry;
// Define comboCapital variable as a ComboBoxColumn column style object of DataGridComboBoxColumn class
internal DataGridComboBoxColumn comboCapital;

...........

// Set column styles
// Set datagrid ComboBox ColumnStyle for Country field
comboCountry = (DataGridComboBoxColumn) new DataGridComboBoxColumn(ref Dictionary, 0, 0, true, false, false, DataGridComboBoxColumn.DisplayModes.ShowDisplayMember, 0);
comboCountry.Leave += new DataGridComboBoxColumn.LeaveEventHandler(this.comboCountry_Leave);
TblStyle.GridColumnStyles.Add(comboCountry);
TblStyle.GridColumnStyles[0].MappingName = "Country";
TblStyle.GridColumnStyles[0].HeaderText = "Country";
TblStyle.GridColumnStyles[0].Width = 165;
TblStyle.GridColumnStyles[0].NullText = string.Empty;

// Set datagrid ComboBox ColumnStyle for Capital field
comboCapital = (DataGridComboBoxColumn) new DataGridComboBoxColumn(ref Dictionary, 1, 1, true, false, false, DataGridComboBoxColumn.DisplayModes.ShowDisplayMember, 0);
comboCapital.Leave += new DataGridComboBoxColumn.LeaveEventHandler(this.comboCapital_Leave);
TblStyle.GridColumnStyles.Add(comboCapital);
TblStyle.GridColumnStyles[1].MappingName = "Capital";
TblStyle.GridColumnStyles[1].HeaderText = "Capital";
TblStyle.GridColumnStyles[1].Width = 165;
TblStyle.GridColumnStyles[1].NullText = string.Empty;

...........

// "Country" datagrid cell has been left. Update corresponding "Capital" cell value in the current datagrid row
private void comboCountry_Leave(object sender, System.EventArgs e)
{
System.Data.DataRow[] Capital = Dictionary.Select("Country='" + comboCountry.combo.Text + "'");
DataGrid1[DataGrid1.CurrentRowIndex, 1] = Capital[0][1];
}

// "Capital" datagrid cell has been left. Update corresponding "Country" cell value in the current datagrid row
private void comboCapital_Leave(object sender, System.EventArgs e)
{
System.Data.DataRow[] Country = Dictionary.Select("Capital='" + comboCapital.combo.Text + "'");
DataGrid1[DataGrid1.CurrentRowIndex, 0] = Country[0][0];
}

Learn more about DataGridColumns assembly

The assembly online documentation: http://www.rustemsoft.com/DataGridColumnsDoc/


Visual Basic News and Information Source - www.VBWire.com




Copyright © 2001-2022 RustemSoft

ODBC compliant data source such as SQL Server, ORACLE, MySQL and many more.
You do not need programming knowledge to use the product. It is purposed at non-technical users who may be not familiar with XML. The interface is designed to promote a step by step approach and hide most of the technical issues.
The more technically capable can enter SQL directly if needed to extract data for conversion. Also there are bunch of options for tailoring the XML output to meet most requirements. You can even write your own XSL and automatically transform the XML produced.
There are two options available for integration into web sites or other systems. There is a GUI executable and a command line utility. Both of them allow execution of predefined transformations.
XML Converter takes care of frequent XML creation with single, easy method support for exporting data to XML. The output file is a well-formed XML from a standard template, and may come (if you need) with a standard eXtensible Style Language (XSL) sheet to render the output to HTML-view using the well-known MS Internet Explorer® and a latest version of Netscape Navigator®. The XSL give you ability to extract, transform and render the content of an XML file. And now this specification is supported by popular of Internet browsers and you do not need a physical transformation your XML document into HTML files.