现在我有一个 DataGridView,它绑定(bind)到一个运行良好的 DataTable。我需要在 DataGridView 上做的是在最后添加一个新列,它是一个 ComboBox,它将根据行的键值动态填充。
我的列是 ID、名称和计数。第 4 列将是一个 ComboBox,它采用 ID 并根据该 ID 在下拉列表中创建值。我目前有一个自定义 ComboBox,它在其构造函数中获取 ID 并以这种方式填充它,但我无法找到将其放入 DataGridView 中的方法。因此,我创建了该自定义 ComboBox 控件的副本并将其放入 DataGridViewComboBoxCell 但我仍然不知道如何将其动态绑定(bind)到表单。我在互联网上搜索并找到了一些例子,但并不完全是我想要做的。
This link显示了我想要做的事情,除了我使用的是 C# 而不是 VB。有什么想法吗?
最佳答案
这是我刚刚尝试过的:
DataTable dt = new DataTable();
dt.Columns.Add("Col");
dt.Rows.Add();
dt.Rows.Add();
dt.Rows.Add();
dt.Rows[0][0] = "1";
dt.Rows[1][0] = "2";
dt.Rows[2][0] = "3";
dataGridView1.DataSource = dt;
dataGridView1.Columns.Add(new DataGridViewComboBoxColumn());
List<string> lstStr = new List<string>();
lstStr.Add("1");
lstStr.Add("2");
lstStr.Add("3");
lstStr.Add("4");
((DataGridViewComboBoxCell)(dataGridView1.Rows[0].Cells[1])).DataSource = lstStr;
这是您要找的吗?
关于c# - 将 DataGridView 绑定(bind)到 DataTable,但还包括 ComboBox 作为最后一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1560964/