0

I have two lists and two ComboBoxes. So, I would like to make a relation between them. That means, when you choose from box1 something, then you can choose only some options in box2, which is related to box1.

Notice:
I'm not creating ComboBox in GUI, I'm using code. It's looking like this: enter image description here

Question:
I need to get value when the user chose something in my ComboBox. How can I get the user's choice?

Code:

DataGridViewComboBoxColumn cmb = new DataGridViewComboBoxColumn();
cmb.Name = "Accounts";
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.Name = "Contacts";
cmb2.DataSource = data;
dataGridView1.Columns.Add(cmb2);

When I run my app: enter image description here

  • I have the same question, try this one: https://stackoverflow.com/questions/5041725/how-to-get-value-from-datagridview-combobox – Sanktos Sep 19 '22 at 17:50

1 Answers1

4

I assume you mean this logic. I hope it won't be difficult for you to adapt it for your template. enter image description here enter image description here

/** required */
using System.Linq;

public Form1()
{
    InitializeComponent();

    var source = new Dictionary<string, string>()
    {
        { "Red",        "Colors"  },
        { "Yellow",     "Colors" },
        { "hasOne",     "Relationships" },
        { "belongsTo",  "Relationships" },
        { "hasMany",    "Relationships" }

    };

    #region DataGridViewComboBoxCell

    var dgcb1 = (DataGridViewComboBoxCell)dataGridView.Rows[0].Cells[0];
    var dgcb2 = (DataGridViewComboBoxCell)dataGridView.Rows[0].Cells[1];

    dgcb1.Items.Clear();
    dgcb1.Items.AddRange(source
        .Select(x => x.Value)
        .Distinct()
        .ToArray());

    dataGridView.CellValueChanged += (s, e) =>
    {
        dgcb2.Items.Clear();
        dgcb2.Items.AddRange(source
            .Where(x => x.Value == dgcb1.Value.ToString())
            .Select(x => x.Key)
            .ToArray());
    };

    #endregion

    #region Combobox 

    cb1.Items.Clear();
    cb1.Items.AddRange(source
        .Select(x => x.Value)
        .Distinct()
        .ToArray());

    cb1.SelectedIndexChanged += (s, e) =>
    {
        cb2.Items.Clear();
        cb2.Items.AddRange(source
            .Where(x => x.Value == cb1.SelectedItem.ToString())
            .Select(x => x.Key)
            .ToArray());
    };

    #endregion
}
  • Hi! I also stuck with ComboBox, but in your explanation, I can not see class DataGridViewComboBoxColumn. Where you found cb1? In my case, I also do not have anything in GUI, so I used the class ComboBox to create an instance and then tried to do some stuff. How do you manage it? – Sanktos Sep 19 '22 at 18:38
  • 2
    @Sanktos I used the default winforms ```ComboBox``` control (cb1, cb2), you can (should) use the ```DataGridViewComboBoxCell```, which also has the ```Items``` field and the ```AddRange``` method for it. To track the value change, use ```CellValueChanged```. – Hopex Development Sep 19 '22 at 19:00
  • @Sanktos I've updated the code in the response a bit – Hopex Development Sep 19 '22 at 19:08
  • @HopexDevelopment Hey, I'm getting an error here: var dgcb1 = (DataGridViewComboBoxCell)dataGridView.Rows[0].Cells[0]; Error is: System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index' – Frederico Eglesias Sep 20 '22 at 07:27
  • @FredericoEglesias Hi, such an error will be your empty table does not contain records and the ```allow addition``` option is disabled. – Hopex Development Sep 20 '22 at 13:46
  • 1
    @FredericoEglesias This is a demo version of the implementation. This is the implementation of the dependence of the elements of the second list on the selection of an element in the first drop-down list. Sorry, the answer is focused on dependency, but not on detailed handling of cells, this is another topic :) – Hopex Development Sep 20 '22 at 13:52