Adding ComboBox to a datagridview for a particular row

后端 未结 2 1770
Happy的楠姐
Happy的楠姐 2021-01-07 00:26

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

2条回答
  •  悲&欢浪女
    2021-01-07 00:33

    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 books = new List();
        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.

提交回复
热议问题