Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Problem in databinding SelectedValue of comboxbox to another DataSet object
 

Problem in databinding SelectedValue of comboxbox to another DataSet object

I bind my ComboxBox to DataSetA to retrieve the name and value information. I also bind the SelectedValue of the ComboBox to a column of DataSetB.

The problem occurs when I have only one item in DataSetA. There is no change event from the ComboBox and the column of DataSetB will get nothing in return.
cwlaualex  Thursday, September 06, 2007 4:58 AM

Hi, cwlaualex,

Based on my understanding, the binding of ComboBox doesn't work if the DataSetA only has one row, does it?

Well, I guess the problem is related to the sequence of DataBinding.

Maybe you can try my sample, and it works for both one row and multiple rows of DataSetA.

Code Snippet

DataSet DataSetA;

DataSet DataSetB;

private void Form1_Load(object sender, EventArgs e)

{

DataSetA = new DataSet();

DataSetB = new DataSet();

DataTable tableA = new DataTable();

tableA.Columns.Add("name");

tableA.Columns.Add("value");

tableA.Rows.Add("abc", 1);

tableA.Rows.Add("bcd", 2);

DataTable tableB = new DataTable();

tableB.Columns.Add("column");

tableB.Rows.Add(5);

DataSetA.Tables.Add(tableA);

DataSetB.Tables.Add(tableB);

comboBox1.DataBindings.Add("SelectedValue", DataSetB.Tables[0], "column", true, DataSourceUpdateMode.OnPropertyChanged);

comboBox1.DisplayMember = "name";

comboBox1.ValueMember = "value";

comboBox1.DataSource = DataSetA.Tables[0];

}

private void button1_Click(object sender, EventArgs e)

{

MessageBox.Show(DataSetB.Tables[0].Rows[0][0].ToString());

}

Use the SelectedValueChanged event instead of SelectedIndexChanged.

http://msdn2.microsoft.com/en-us/library/system.windows.forms.listcontrol.selectedvaluechanged.aspx

Ken Tucker  Thursday, September 06, 2007 9:28 AM
Hi Ken,

My problem is not from the SelectedValueChanged or SelectedIndexChanged event. It's all about databinding. Actually, I don't have any handlers for these two events. I just want the SelectedValue update the column in my DataSetB with the selected value in the combo box. Moreover, it fails to work only when the combo box does not change after loaded.

Regards,
Alex
cwlaualex  Friday, September 07, 2007 2:00 AM

Ok I understand what you are looking to do now. We can create a datarelation which when bound properly will automatically update combobox. The problem is the datarelation need to be setup between datatables in a dataset. If you could move the datatable in datasetb into dataseta. We can create the datarelation.

http://www.vb-tips.com/MasterDetailBindingSource.aspx

http://www.vb-tips.com/MasterDetailNoBindingSource.aspx

Ken Tucker  Friday, September 07, 2007 10:37 AM

Hi, cwlaualex,

Based on my understanding, the binding of ComboBox doesn't work if the DataSetA only has one row, does it?

Well, I guess the problem is related to the sequence of DataBinding.

Maybe you can try my sample, and it works for both one row and multiple rows of DataSetA.

Code Snippet

DataSet DataSetA;

DataSet DataSetB;

private void Form1_Load(object sender, EventArgs e)

{

DataSetA = new DataSet();

DataSetB = new DataSet();

DataTable tableA = new DataTable();

tableA.Columns.Add("name");

tableA.Columns.Add("value");

tableA.Rows.Add("abc", 1);

tableA.Rows.Add("bcd", 2);

DataTable tableB = new DataTable();

tableB.Columns.Add("column");

tableB.Rows.Add(5);

DataSetA.Tables.Add(tableA);

DataSetB.Tables.Add(tableB);

comboBox1.DataBindings.Add("SelectedValue", DataSetB.Tables[0], "column", true, DataSourceUpdateMode.OnPropertyChanged);

comboBox1.DisplayMember = "name";

comboBox1.ValueMember = "value";

comboBox1.DataSource = DataSetA.Tables[0];

}

private void button1_Click(object sender, EventArgs e)

{

MessageBox.Show(DataSetB.Tables[0].Rows[0][0].ToString());

}

You can use google to search for other answers

Custom Search

More Threads

• Can I extend methods of Double Class ?
• Custom button column style in datagrid causes error
• Identity problem in inserting data into master and detail tables
• A data Grid with results depending on User Input
• How to set the TAB Control visible
• Create a custom DataGridViewColumn for radio buttons
• DataGrid and columns sizing
• import selected row from datagridview to excel
• Show ReadOnly DataGridViewCheckBoxColumn CheckBoxes as Grey'd out.
• Formatting entries in DataGridView selection