0

I am desperately trying to add a DataGridViewComboBoxColumn to my DataGridView programatically. Here is the Code I've got so far.

DataTable dt = new DataTable();
dt = _userBL.getUsersTable().DefaultView.ToTable(false, "Person_ID", "FirstName", "LastName", "Title", "Username");
dataGridView1.DataSource = dt;

DataGridViewComboBoxColumn ComboBoxCell = new DataGridViewComboBoxColumn();
ComboBoxCell.Name = "State";
ComboBoxCell.ValueMember = "State";//ComboBoxCell.DisplayMember = "State";
ComboBoxCell.DisplayMember = "State"; 
ComboBoxCell.DataSource = _userBL.getUsersTable().DefaultView.ToTable(false, "State");
this.dataGridView1.Columns.Add(ComboBoxCell);

But i get no data in the comboboxcolumn, and since I added DataSource, I cannot add items (Active,Inactive) in it. Also, when I click it a small dropdown should appear, even for empty comboboxes, but it does not. Thanks in advance!

Tanatos Daniel
  • 558
  • 2
  • 9
  • 27

1 Answers1

0

Try this one..

DataGridViewComboBoxColumn combo = (DataGridViewComboBoxColumn)dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[1].OwningColumn;
            dt.Clear();
            da = new SqlDataAdapter("select name from tblData", con);
            da.Fill(dt);
            combo.DataSource = dt;
            combo.DisplayMember = "name";
user3502966
  • 59
  • 2
  • 14
  • I'm getting unable to cast from DataGridViewTextBoxColumn to DataGridViewComboBoxColumn(your 1st line). Also, I do not understand why did you set the indexes the way you did. – Tanatos Daniel Apr 06 '14 at 09:43
  • My code also works, but because i had my datagridview,editmode = editprogramatically, i couldn't access dropdown list. – Tanatos Daniel Apr 06 '14 at 16:58