0
votes

I have 2 tables:

First:

id | 2ndTableId | Name

Second:

id | name

I want them to show on dataGridView as columns: 1 -> ComboBox with list of names from 2nd table (with some where clause), selected value = from 1st table '2ndTableId' 2-> name from first table

Moreover I want to keep in ComboBox 2 values (id and name). I made own class MyComboBox with Id and Name properties, but I got an error:

System.argumentException: DataGridViewComboBoxCell values is not valid.

I have no idea how to manage this. Can you help me?

1

1 Answers

0
votes

I had a same scenario. I created the two DTO classes for two tables and assign them. Create two classes

public class Table1
{
    public int Id { get; set; }
    public int Table2Id { get; set; }
    public string Name { get; set; }
}

public class Table2
{
    public int Table2Id { get; set; }
    public string Table2Name { get; set; }
}

Then in you from create a datagridview with four columns.

Column1=> Name:idDropDown Default Text Style:DataGridViewCellStyle { }
Column 2=> Name:Table1Id DataPropertyName:Table1Id 
Column 3=> Name:Table2Id DataPropertyName:Table2Id 
Column 4=> Name:Tbale1Name DataPropertyName:Tbale1Name 

Then generate two lists with the data what you require.

List<Table1> dropDownList;
List<Table2> gridData;

In you code use as follows

idDropDown.DisplayMember="Table2Name";
idDropDown.ValueMember="Table2Id";
idDropDown.DataSource=dropDownList;

gridview1.AutoGenerateColumns = false;
gridview1.DataSource=gridData;

The main thing is we have to set AutoGenerateColumns to False in this code