将ComboBox添加到特定行的datagridview [英] Adding ComboBox to a datagridview for a particular row
问题描述
我有一个要求,显示下拉列表到几行之间的一个特定行。
我的dataGridView有2列(参数和值),我在代码中动态添加3行。对于所有3行,参数文本是固定的,不能被用户修改。对于第一行,参数文本为prm1,值文本将从下拉列表中选择。其他2行,值文本将由用户输入的文本框。我尝试搜索,但找不到答案。请帮助我在这里。
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 in code. For all 3 rows, paramter texts are fixed and can't be modified by user. For first row, Parameter text is "prm1" and Value text will be chosen from drop down list. other 2 rows, Value text will be text box entered by User. I tried searching but couldn't find the answer. Please help me here.
推荐答案
可以在运行时替换特定的DataGridView单元格 - 在您的情况下,例如您想要的一个单元格中的组合框可以有一个DataGridViewTextBoxColumn并替换第一行中的单元格。这样的东西:
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;
}
您也可以以其他方式执行此操作,并将其替换为特定的组合框单元格DataGridViewComboBoxColumn与DataGridViewTextBoxCell。
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.
这篇关于将ComboBox添加到特定行的datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!