how to show combobox in datagridview?

87 Views Asked by At

Hello everyone I have two Tables named Brand(Id,Name) and Product(Id,BrandId,Name), I want showing Product list in DataGridView(Id,Name,Brand), Brand Column should be ComboBox that refer to BrandId in Product Table and shows Name Of Brand from records in Brand table

How can do it

Brand Table

enter image description here

Product Table

enter image description here

DataGridView

enter image description here

1

There are 1 best solutions below

1
Aleksa Ristic On
// Create and populate DataTable

DataTable dt = new DataTable();
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Brand", typeof(string));

// Populate it here

// With this once we set datasource of dgv, it will not generate columns automatically
dataGridView1.AutoGenerateColumns = false;

DataGridViewTextBoxColumn idCol = new DataGridViewTextBoxColumn()
{
    Name = "Id",
    HeaderText = "Id",
    DataPropertyName = "Id",
    Width = 50,
    HeaderCell = new DataGridViewColumnHeaderCell()
    {
        Value = "Id"
    }
};

DataGridViewTextBoxColumn nameCol = new DataGridViewTextBoxColumn()
{
    Name = "Name",
    HeaderText = "Name",
    DataPropertyName = "Name",
    Width = 200,
    HeaderCell = new DataGridViewColumnHeaderCell()
    {
        Value = "Name"
    }
};

DataGridViewComboBoxColumn brandCol = new DataGridViewComboBoxColumn()
{
    Name = "Brand",
    HeaderText = "Brand",
    DataPropertyName = "Brand",
    Width = 100,
    HeaderCell = new DataGridViewColumnHeaderCell()
    {
        Value = "Brand"
    },
    DataSource = new List<Tuple<int, string>>() {
        new Tuple<int, string>(1, "Brand 1"),
        new Tuple<int, string>(2, "Brand 2"),
        new Tuple<int, string>(3, "Brand 3")
    },
    DisplayMember = "Item2",
    ValueMember = "Item1"
};

// Now we add these columns to our dgv
dataGridView1.Columns.Add(idCol);
dataGridView1.Columns.Add(nameCol);
dataGridView1.Columns.Add(brandCol);

// and at last, we bind our data to it
dataGridView1.DataSource = dt;

You can see that column which is our ComboBox use same binding system as normal combobox. For this purposle I used list of tuples to populate it, but you would normally load data from database for that part.

Lets say you have Brand class like this:

public class Brand
{
    public int ID { get; set; } // important to be property, not variable
    public string Name { get; set; }
}

You load data from database and create list of these objects:

// I have done it manually
List<Brand> brandsList = new List<Brand>();
brandsList.Add(new Brand() { ID = 1, Name = "Brand 1 });
brandsList.Add(new Brand() { ID = 2, Name = "Brand 2 });
brandsList.Add(new Brand() { ID = 3, Name = "Brand 3 });

Then you would change upper combobox column definition to:

DataGridViewComboBoxColumn brandCol = new DataGridViewComboBoxColumn()
{
    Name = "Brand",
    HeaderText = "Brand",
    DataPropertyName = "Brand",
    Width = 100,
    HeaderCell = new DataGridViewColumnHeaderCell()
    {
        Value = "Brand"
    },
    DataSource = brandsList,
    DisplayMember = "Name",
    ValueMember = "ID"
};