0

I have a datagridview, in this datagirdview, i added two unbound columns, one is a combobox and second is a textbox type column, besides this , I have a datatable of some rows.

I am setting gird datasource with my datatable, then I am trying to add values to combobox control, it should display three values (Truck, Plane, Ship)

when grid loads it displays stores, but no value in combobox, I tried various methods like creating a combobox data column and adding values to it by binding a datatable, I also tried by adding in column.items both in designer and by code but combobox never gets the values.

as an experiment I am also trying to add a new column by below code, but second column also show no values.

DataGridViewComboBoxColumn dgvcbc = new DataGridViewComboBoxColumn();
        DataTable dt = new DataTable();
        dt.Columns.Add("Media", typeof(string));
        dt.Rows.Add("Truck");
        dt.Rows.Add("Car");
        dgvcbc.DataSource = dt;
        dgvcbc.DisplayMember = "Media";
        dgvcbc.ValueMember = "Media";
        this.grdDestShops.Columns.Add(dgvcbc);
Matthew Walton
  • 9,809
  • 3
  • 27
  • 36
alphaprolix
  • 601
  • 2
  • 10
  • 25

2 Answers2

0

your code is adding a new column!, If you have added a ComboBox columns in the design mode, why not you find it using FindControl function and then bound it?

Alaa Alweish
  • 8,904
  • 16
  • 57
  • 84
0

Think this solves ur problem..

namespace activator
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
       {
        DataTable dt = new DataTable();
        dt.Columns.Add("Media", typeof(string));
        dt.Rows.Add("Truck");
        dt.Rows.Add("Car");            
        ComboBox combo = new ComboBox();
        List<string> media=(from x in dt.AsEnumerable()
                            select x.Field<string>(0)).ToList();
        combo.DataSource = media;
        dataGridView1.Controls.Add(combo);           
    }
  }
}
Sabyasachi Mishra
  • 1,677
  • 2
  • 31
  • 49
Bharath
  • 195
  • 1
  • 1
  • 19