0

I have a problem with DataGridViewComboBoxColumn. I have two of those and when I'm selecting something in one, then I would like to see only things which related to that one.

enter image description here

My code:

public Form1()
{
    InitializeComponent();

    List<string> contactNames = accountNamez.Select(s => (string)s).ToList();
   
    dataGridView1.ColumnCount = 2;
    dataGridView1.Columns[0].Name = "Product ID";
   
    DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
    List<string> data = new List<string>();
    foreach (var item in contactNames)
    {
        data.Add(item);
    }
    cmb.DataSource = data;
    dataGridView1.Columns.Add(cmb);
    //dataGridView1.Rows.Add(data);

    DataGridViewComboBoxColumn cmb2 = new DataGridViewComboBoxColumn();
    List<String> contacts2 = new List<String>();

    cmb2.DataSource = data;
    dataGridView1.Columns.Add(cmb2);e
}

Do I need to make some event handler? I'm new to it. I'm not sure how to make relations between two lists with DRVComboBoxColumn.

Jimi
  • 29,621
  • 8
  • 43
  • 61
  • "then I would like to see only things which related to that one." where do you want to see that? in the entire second ComboBoxColumn? – Mong Zhu Sep 16 '22 at 12:32
  • @MongZhu, Yes, sorry, for the misunderstanding. I have the second list, which depends on what you choose from the first one, but I don't have any ideas on how to implement it. – Frederico Eglesias Sep 16 '22 at 12:40
  • "which depends on what you choose from the first one" please post the filter logic, what shall end up in the second datasource list? But basically you would filter, then populate the second list and then attach the second list as `DataSource` to the second ComboBoxColumn – Mong Zhu Sep 16 '22 at 12:48
  • Make a class with two properties (Product ID and Contact Name) so you have an association. – jdweng Sep 16 '22 at 12:53
  • @MongZhu For example we have two boxes. In the first one, we need to choose an account. When we chose an account then in the next box we can choose contacts that only have relations to that account. How can I make this stuff work? I mean how can I connect two boxes? – Frederico Eglesias Sep 16 '22 at 12:54
  • @FredericoEglesias ok, you have a DataGridView with many rows. So if you select in the first row a certain account, then you want in the second column to have contacts that only have relations to that account in **all** rows?? I doubt that.... "How can I make this stuff work?" you would take the selected item from the combobox in question and use it to filter all contacts. [this post](https://stackoverflow.com/a/51908848/5174469) shows how to get the selected item. Please post the code that will filter the contacts list – Mong Zhu Sep 16 '22 at 13:30
  • @MongZhu Thanks for the reply, could you please tell me then - how can I get value from the first box? I chose something in the first box, how can I get a value of that? – Frederico Eglesias Sep 16 '22 at 13:47
  • "how can I get a value of that? " I posted you a link in my last comment that shows how to do that – Mong Zhu Sep 16 '22 at 13:56

0 Answers0