Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Data binding in Win Forms
 

Data binding in Win Forms

Seems like a really easy question, but I'll ask it anyway: I drive the population of one combo box based on the selected value of another combo box all the time in ASP.NET at design time by binding the first's required parameter to the second control. I'm trying to do the same thing in Win Forms but cannot seem to find out how to do it. Like I said, probably WinForms 101 but I'm new to them, so thanks for any help you can provide ...
bobness  Wednesday, August 05, 2009 5:23 AM

Hi bobness,

We can create a BindingSource and bind it to comboBox2 whose data source based on the selected value of comboBox1. This is a code snippet:
BindingSource bindSrc = new BindingSource();

bindSrc.DataSource = GetComboBox2DataSource();

comboBox2.DataSource = bindSrc;

Then we need to handle the SelectedValueChanged event to filter the data source of comboBox2. This is a code snippet:
const string FILTER_COL_NAME = "col1";

void comboBox1_SelectedValueChanged(object sender, EventArgs e)

{

BindingSource bindSrc = comboBox2.DataSource as BindingSource;

bindSrc.Filter = string.Format("{0} = '{1}'", FILTER_COL_NAME,comboBox1.SelectedValue);

}

This is a sample shows how to bind data to a ComboBox:
http://msdn.microsoft.com/en-us/library/x8ybe6s2(VS.80).aspx.

Let me know if this helps.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Thursday, August 06, 2009 7:40 AM

Hi bobness,

We can create a BindingSource and bind it to comboBox2 whose data source based on the selected value of comboBox1. This is a code snippet:
BindingSource bindSrc = new BindingSource();

bindSrc.DataSource = GetComboBox2DataSource();

comboBox2.DataSource = bindSrc;

Then we need to handle the SelectedValueChanged event to filter the data source of comboBox2. This is a code snippet:
const string FILTER_COL_NAME = "col1";

void comboBox1_SelectedValueChanged(object sender, EventArgs e)

{

BindingSource bindSrc = comboBox2.DataSource as BindingSource;

bindSrc.Filter = string.Format("{0} = '{1}'", FILTER_COL_NAME,comboBox1.SelectedValue);

}

This is a sample shows how to bind data to a ComboBox:
http://msdn.microsoft.com/en-us/library/x8ybe6s2(VS.80).aspx.

Let me know if this helps.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Thursday, August 06, 2009 7:40 AM

You can use google to search for other answers

Custom Search

More Threads

• How can I find Designer error to fix it?
• upgrading windows user control from VS 2003 to VS 2005
• UndoEngine in standalone Designer
• Visual studio resizes forms
• refresh properties window for dynamic properties
• How to register an activex control
• How do I reference a control's properties?
• Accented characters in MyForm.designer.vb not loaded correctly by VS!?
• Create a Select Resource Dialog Box
• getting the designer to keep its paws off of my custom control properties?