Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello Friends !!!!

I have DataGridView which contains two ComboBox columns. The second ComboBox will be filled with data depending on the selected value from first ComboBox On C#

Below is my code


C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace t1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            logicload(2, 2);
        }
        public void logicload(int row, int col)
        {

            dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
            for (int i = 1; i <= col; i++)
            {
                dataGridView1.Columns.Add("Col" + i, "Stage" + i);
            }

            dataGridView1.Rows.Add(row);

            //make row 1 at all columns into combobox cell.
            dataGridView1.Rows[0].Cells[1] = new DataGridViewComboBoxCell();
            dataGridView1.Rows[1].Cells[1] = new DataGridViewComboBoxCell();
            dataGridView1.Rows[0].Cells[0].Value = "Country";
            dataGridView1.Rows[1].Cells[0].Value = "City";

            loadComboboxesSampleItems();
        }
        DataGridViewComboBoxCell CellColumn1, CellColumn2;
        private void loadComboboxesSampleItems()
        {

            CellColumn1 = (DataGridViewComboBoxCell)this.dataGridView1.Rows[0].Cells[1];
             CellColumn2 = (DataGridViewComboBoxCell)this.dataGridView1.Rows[1].Cells[1];
            var list1 = new List<string>() { "IND", "PAK", "AUS", "USA" };
            var list2 = new List<string>() { "AGRA", "DEHLI", "KANPUR" };
            var list3 = new List<string>() { "MOhali" };

            CellColumn1.DataSource = list1;// CellColumn2.DataSource = list2;
        }
        private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
Posted
Comments
V. 22-May-14 3:50am    
And what is the question? There is no question in your post...

What you need to do is catch the CellValueChanged event for the first combo box, and in it you repopulate the values in second combo box.

Check out:

http://stackoverflow.com/questions/11950189/how-to-handle-selectedindexchanged-event-for-a-combobox
 
Share this answer
 
Hi,
I have a code snippet here, but the data isn't from the datagrid. This one is kinda simple. Hope it will help you to come up with the good logic.
C#
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboCountry.Text == "Philippines")
            {
                cboProvince.Items.Clear();
                cboProvince.Items.Add("Batangas");
                cboProvince.Items.Add("Manila");
            }
            else if (cboCountry.Text == "Japan")
            {
                cboProvince.Items.Clear();
                cboProvince.Items.Add("Kyoto");
                cboProvince.Items.Add("Tokyo");
            }
            else if (cboCountry.Text == "South Korea")
            {
                cboProvince.Items.Clear();
                cboProvince.Items.Add("Seoul");
                cboProvince.Items.Add("Busan");
            }
        }


This kind of code isn't wise to use for larger data. But I'm just giving you the logic. It's up to you on how you will come up with your solution.
You know, coding with a little help from others and you coming up with more good solution is such a great feeling. Hope it helps. :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900