I have two Combo Boxes..1-Programme 2-Semester
I want to bind Semester combo box with Programme combo box..

i m reading file from Xml....
after binding i have to show data on datagrid view...
but i m not able to do it......

i hope someone will help me...
Regards..
Farhad

Recommended Answers

All 2 Replies

Check this out:

PLEASE: take time and study the example. Code works, you only have to adapt to your needs.

public partial class Form1 : Form
    {
        bool bSkipping;
        DataTable table;
        public Form1()
        {
            InitializeComponent();         
            string[] semesters = { "1st", "2nd", "3rd" };
            comboBox1.DataSource = semesters;
            comboBox1.DisplayMember = "Value";
            comboBox1.SelectedIndex = -1;

            comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
            comboBox2.SelectedIndexChanged += new EventHandler(comboBox2_SelectedIndexChanged);
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //RESET dataTable
            if (table != null)
            {
                table.Clear();
                table = null;
            }

            string[] programme = null;
            bSkipping = true;
            switch (comboBox1.SelectedItem.ToString())
            {
                case "1st":
                    programme = new string[] { "Programme A", "Programme B", "Programme C" };
                    break;
                case "2nd":
                    programme = new string[] { "Programme D", "Programme E", "Programme F" };
                    break;
                case "3rd":
                    programme = new string[] { "Programme G", "Programme H", "Programme I" };
                    break;
                default:
                    break;
            }
            comboBox2.DataSource = programme;
            comboBox2.DisplayMember = "value";
            comboBox2.SelectedIndex = -1;
            bSkipping = false;
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!bSkipping)
            {
                //I have only my own code to populate DGV: you have to get yours:
                PopulateDGV(comboBox2.SelectedItem.ToString());
            }
        }

        private void PopulateDGV(string value)
        {
            //populate with some sample data
            //you will have to get your data by user self from somewhere!
            //THIS code only shows data in DGV when you select:
            //1st and Programme A,
            //2nd and Programme D

            //RESET dataTable:
            if (table != null)
            {
                table.Clear();
                table = null;
            }

            table = new DataTable();
            table.Columns.Add("Column1", typeof(int));
            table.Columns.Add("Column2", typeof(int));
            table.Columns.Add("Column3", typeof(int));

            DataRow dr;
            switch (value)
            {
                case "Programme A":
                    {
                        for (int i = 1; i < 6; i++)
                        {
                            dr = table.NewRow();
                            dr["Column1"] = i;
                            dr["Column2"] = i + 2;
                            dr["Column3"] = i * 2 + 2;
                            table.Rows.Add(dr);                            
                        }
                        break;
                    }
                case "Programme D":
                    {
                        for (int i = 1; i < 6; i++)
                        {
                            dr = table.NewRow();
                            dr["Column1"] = i + 2;
                            dr["Column2"] = i + 2 / 2;
                            dr["Column3"] = i * 4;
                            table.Rows.Add(dr);                            
                        }
                        break;
                    }
                default:
                    break;
            }
            //binding data in the dataTable to DGV:
            dataGridView1.DataSource = new BindingSource(table, null);
            dataGridView1.AllowUserToAddRows = false;
            dataGridView1.RowHeadersVisible = false;
        }
    }

Hope it helps,
Mitja

According to me, You will get best way to implement the data binding in this code

class FeedCollection : BindingList<Feed>
{
private Control _control;

//The method is called when data is updated (for ex. data comes from TCP/IP)
void OnUpdateReceiced(...)
{
    _control.Invoke(new MethodInvoker(delegate
    {
        //Raise notification in GUI thread
        OnListChanged(new ListChangedEventArgs(...));
    }));
}

}

I hope this will helps you and for further reference visit on www.dapfor.com

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.