Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > DataGridViewComboBoxColumn selected value problem
 

DataGridViewComboBoxColumn selected value problem

Hello,

I have DataGridView containing DataGridViewComboBoxColumn. I generate a dataset1 and i fill this column comboboxwith the table from the dataset1. Now the DataGridView is filled with the table from another dataset, dataset2, it contains a column that should be the selected value in the DataGridViewCombobox column. How do I set this? How do i set the selected value of the combobox column is from the dataTable that DataGridView is taking the data from?

Thanks

Barbi Rio  Friday, October 10, 2008 7:07 AM

Thanks Kira but I found better soluton.

The DataGridViewComboBoxColumn has DataSource containing 2 colums, one isset asthe displayMember and the other is the ValueMember. Also DataPropertyName has to be set to the column from the dataset used to fill the DataGridView.

The important thing is that DisplayMember, ValueMember and DataPropertyName are of datatype String. So in the SQL Query for the DataGridView, the column name in DataPropertyName has to be String.

Also here is a link that helped me a lot: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcolumn.aspx

Barbi Rio  Monday, October 27, 2008 7:06 AM

Hi Barbi

I have written an example for you to implement this.

Code Snippet

public partial class Form1 : Form

private DataSet ds1 = new DataSet();

private DataSet ds2 = new DataSet();

private DataTable dtSource = new DataTable("dtSource");

private DataTable dtComboBox = new DataTable("dtComboBox");

private ComboBox cmb;

public Form1()

"Value");

"Name");

"Item1");

"Item2");

"Item3");

"Item4");

"Column1");

"Column2");

"test1");

"test2");

"test3");

"test4");

DataGridViewComboBoxColumn dgvComboBox = new DataGridViewComboBoxColumn();

"Name";

"Column1_Name";

"Value";

"Column1";

"dtComboBox"];

"dtSource";

DataGridViewTextBoxColumn dgvText = new DataGridViewTextBoxColumn();

"Column1_Value";

new DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);

new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);

void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)

if (e.RowIndex < dtSource.Rows.Count)

void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)

as ComboBox;

if (cmb != null)

new EventHandler(cmb_SelectedValueChanged);

void cmb_SelectedValueChanged(object sender, EventArgs e)

this.dataGridView1[0, dataGridView1.CurrentRow.Index].Value = cmb.SelectedValue;

The CellFormatting event of the DataGridView allow you to fill the Column1_Value at the first run. Then you can handle the EditingControlShowing event of the DataGridView to set the first column(Column_Value) cell value according to the ComboBox select action.

Sincerely,

Kira Qian

Windows Forms General FAQs
Windows Forms Data Controls and Databinding FAQs

Kira Qian  Monday, October 13, 2008 9:39 AM

Adding event handlers just ot initialize selected item? This is too much. In asp.net (even in 2003)is much much easier, with 2 rows of code this can be acheaved. It can't be the only solution.

Barbi Rio  Friday, October 24, 2008 6:24 AM

My solution is handle the EditingControlShowing event of DataGridView and SelectedValueChanged event of ComboBox. A lot of code is just for test. The key point is the bold font. If you want to simplify it, then the CellFormatting event is not needed. The solution of Winform may be different with ASP.NET.

Sincerely,

Kira Qian

Kira Qian  Friday, October 24, 2008 6:47 AM

Thanks Kira but I found better soluton.

The DataGridViewComboBoxColumn has DataSource containing 2 colums, one isset asthe displayMember and the other is the ValueMember. Also DataPropertyName has to be set to the column from the dataset used to fill the DataGridView.

The important thing is that DisplayMember, ValueMember and DataPropertyName are of datatype String. So in the SQL Query for the DataGridView, the column name in DataPropertyName has to be String.

Also here is a link that helped me a lot: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcolumn.aspx

Barbi Rio  Monday, October 27, 2008 7:06 AM

Thank you for sharing your experience Barbi, it will be useful to the person who view this thread.

Kira Qian  Monday, October 27, 2008 7:11 AM

You can use google to search for other answers

Custom Search

More Threads

• How to bind a custom business object that contains other business objects?
• How do you get the whole error icon to be displayed
• why the binded datasource not changed?
• Advice needed on best practices approach using dataset/BindingSource
• bind multiple entries to the one textbox
• saving image in folder system and filepath in database vbnet
• How to Bind a Calendar to a DetailsView,DaatGridView and Form View?
• Extending the functionality of the table classes generated by Visual Studio Dataset Designer
• Active Filtering/Sorting BindingListView
• Display decrypted data from encrypted values in a Datagrid