Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > how to refresh combobox in datagridview in vb related to another combobox in the same gridview
 

how to refresh combobox in datagridview in vb related to another combobox in the same gridview

I have a form with datagridview as mydv
i use dataset
inside mydv there are tow combobox combox1 and combox2
combobox1 data from table tb1(col1,col2)
combobox2 data from table tb2(col1,col2,col3,col4)
col1 in tb2 is a forign key related to tb1(col1),so when i change combobox1 i want get data for selected value(col1)
can you help me.

  • Moved byeryangMSFTMonday, October 05, 2009 9:10 AM (From:Building Development and Diagnostic Tools for .Net)
  •  
RAOOFH  Sunday, October 04, 2009 10:50 AM

Hi RAOOFH,

If the relation between two tables are of type master/detail, you can bind the main table to a ComboBox and bind the relation of the tables to another ComboBox. You can follow the document below which did not show the answer directly but can provide you so many ideas:
http://msdn.microsoft.com/en-us/library/aa984462(VS.71).aspx

You can also create two BindingSources the data sources of which are the two tables and bind each to a ComboBox. Then you can handle the SelectedIndexChanged event of one ComboBox. In the handler, you need to get the selected value and take it as a parameter to call the Find method of the BindingSource of the second ComboBox. When  find one record, you can set the Position of the BindingSource to synchronize the two ComboBoxes. This is a code snippet:

        private DataTable table1;
        private DataTable table2;
        private BindingSource bindSrc1;
        private BindingSource bindSrc2;
        private void Form5_Load(object sender, EventArgs e)
        {
            //Fill table11 and table2.
            table1 = GetTable1();
            table2 = GetTable2();
            //Create and bind the BindingSources.
            bindSrc1 = new BindingSource();
            bindSrc1.DataSource = table1;
            this.comboBox1.DataSource = bindSrc1;
            bindSrc2 = new BindingSource();
            bindSrc2.DataSource = table2;
            this.comboBox2.DataSource = bindSrc2;
            //Handle this event to synchronize the selected item.
            this.comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
        }
        void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex != -1)
            {
                DataRowView row = bindSrc1.Current as DataRowView;
                //Find the related item and modify the current item of bindingsource2.
                int index = bindSrc2.Find("col1",row["col1"]);
                if (index != -1)
                {
                    bindSrc2.Position = index;
                }
            }
        }



Let me know if this does not help.
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  Wednesday, October 07, 2009 7:11 AM

You can use google to search for other answers

Custom Search

More Threads

• How to add duplicate datacolumn to datatable
• DataGridView & ODBC
• Where is the DataForm Wizard?
• Custom Business Object Data Binding Warning
• ComboBox ValueMember Binding Issue
• BindingSource and Deep Properties
• How can we do diffrent alternate color row in msflexgrid
• This forum has moved the week of 12/8/2008
• I want some rows read-only in datagridview Edit mode
• Datagridview will not draw itself for certain users