I have a requirement to show drop down list to one particular row among several rows. My dataGridView has 2 columns (Parameter and Value) and I am adding 3 rows dynamically
It is possible to replace specific DataGridView cells at run time - in your situation for example where you want a combobox in one cell you can have a DataGridViewTextBoxColumn and replace the cell in the first row. Something like this:
void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
List<Book> books = new List<Book>();
books.Add(new Book { bookID = 1, bookName = "Test-Driven Development (Kent Beck)" });
books.Add(new Book { bookID = 2, bookName = "Refactoring (Martin Fowler)" });
books.Add(new Book { bookID = 3, bookName = "Code Complete: 2nd Edition (Steve McConnell)" });
DataGridViewComboBoxCell c = new DataGridViewComboBoxCell();
c.DataSource = books;
c.Value = 1;
c.ValueMember = "bookID";
c.DisplayMember = "bookName";
dataGridView1.Rows[0].Cells[0] = c;
}
You can also do this the other way and replace a particular combobox cell from a DataGridViewComboBoxColumn with a DataGridViewTextBoxCell.
One thing though - while this will work, better usability might just come from setting some combo boxes to read only.
Please consider adding of ComboBox column to grid with right-click context menu item Edit Columns... of the grid in Form Designer. To add column it will require to click on Add button of Edit Columns dialog and subsequently select Type = DataGridViewComboBoxColumn
as shown below:
Please note: You will also need to use either Items or DataSource property of the column to populate drop-down list of the ComboBox.