Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have DataGirdView with two DataGridViewComboBoxColumn, the first column for countries, and the second one for cities. The second column must be populated with the cities that are actually in the country selected in the first column.
What I tried is to change the DataSource for the second column, but every time when the DataSource changed, all the cells in cities' column become blank except the cities bind on the current country that appears in countries' column.

What I have tried:

C#
public partial class Form1 : Form
{
    DataGridViewComboBoxColumn column;
    DataTable dataTable1 = new DataTable("Country1");
    DataTable dataTable2 = new DataTable("Country2");
    public Form1()
    {
        InitializeComponent();
        column = new DataGridViewComboBoxColumn();

        dataTable1.Columns.Add("Code", typeof(String));
        dataTable1.Columns.Add("Cities", typeof(String));

        dataTable1.Rows.Add("1", "City1 of country1");
        dataTable1.Rows.Add("11", "City2 of country1");
        dataTable1.Rows.Add("111", "City3 of country1");

        dataTable2.Columns.Add("Code", typeof(String));
        dataTable2.Columns.Add("Cities", typeof(String));

        dataTable2.Rows.Add("2", "City1 of country2");
        dataTable2.Rows.Add("22", "City2 of country2");
        dataTable2.Rows.Add("222", "City3 of country2");

        dataGridView1.Columns.Add(column);

        column.DataSource = dataTable1;
        column.ValueMember = "Code";
        column.DisplayMember = "Cities";
    }
    private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        if(e.ColumnIndex==0)
        {
            if(dataGridView1.CurrentCell.Value.ToString()== "Country1")
            {
                column.DataSource = dataTable1;
            }
            else if(dataGridView1.CurrentCell.Value.ToString() == "Country2")
            {
                column.DataSource = dataTable2;
            }
        }
    }
    private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
    {
    }

}
Posted
Updated 22-Oct-19 11:06am
Comments
[no name] 21-Oct-19 18:06pm    
I don't see any code relating to your "problem"; you want someone to write it for you.
B-Koronfol 22-Oct-19 16:56pm    
If you can provide some information! Provide it, but, if you can't! you can ignore the question without gossip.

1 solution

I got the information, using the DataGridViewComboBoxCell.DataSource instade of DataGridViewComboBoxColumn.DataSource will solve the problem.


public partial class Form1 : Form
    {
        DataGridViewComboBoxColumn column;
        DataGridViewComboBoxCell cell;
        DataTable dataTable1 = new DataTable("Syria");
        DataTable dataTable2 = new DataTable("Yemen");
        public Form1()
        {
            InitializeComponent();
            column = new DataGridViewComboBoxColumn();

            dataTable1.Columns.Add("Code", typeof(String));
            dataTable1.Columns.Add("Cities", typeof(String));

            dataTable1.Rows.Add("1", "Damascus");
            dataTable1.Rows.Add("11", "Azaz");
            dataTable1.Rows.Add("111", "Aleppo");

            dataTable2.Columns.Add("Code", typeof(String));
            dataTable2.Columns.Add("Cities", typeof(String));

            dataTable2.Rows.Add("2", "Sanaa");
            dataTable2.Rows.Add("22", "Aden");

            dataGridView1.Columns.Add(column);

            cell = (DataGridViewComboBoxCell)dataGridView1.Rows[0].Cells[1];

            cell.DataSource = dataTable1;
            cell.ValueMember = "Code";
            cell.DisplayMember = "Cities";

            column.CellTemplate = cell;


        }
        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            cell =(DataGridViewComboBoxCell) dataGridView1.Rows[e.RowIndex].Cells[column.Name.ToString()];
            if (e.ColumnIndex==0)
            {
                if(dataGridView1.CurrentCell.Value.ToString()== "Syria")
                {

                    cell.DataSource = dataTable1;
                }
                else if(dataGridView1.CurrentCell.Value.ToString() == "Yemen")
                {
                    cell.DataSource = dataTable2;
                }
            }
        }
    }
}
 
Share this answer
 
v2

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