DataGridView组合框,每个单元格使用不同的dataSources [英] DataGridView comboBox with different dataSources for each cell
问题描述
我试图创建一个保存配置信息的DataGridView。
I am trying to create a DataGridView that holds configuration information.
列中每一行的可用值可以根据不同列中的值而改变,我不能将单个数据源附加到comboBox列。例如:如果您选择car,则availalbe颜色应限制为该模型可用的颜色。
The available values can change for each row within a column based on the values in a different column so I can't attach a single datasource to the comboBox column. As an example: If you select car, the availalbe colors should be limited to colors available for that model.
Car ColorsAvailable
Camry {white,black}
CRV {white,black}
Pilot {silver,sage}
考虑dataGridView的原因是,操作员可以为其他汽车添加行。
The reason for considering the dataGridView is so that the operator can add rows for additional cars.
实现这种类型的UI的好设计是什么?
What is a good design to implement this type of a UI?
推荐答案
您可以在每个 DataGridViewComboBoxCell
上分别设置 DataSource
:
You can set the DataSource
separately on each DataGridViewComboBoxCell
:
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0) // presuming "car" in first column
{ // presuming "ColorsAvailable" in second column
var cbCell = dataGridView1.Rows[e.RowIndex].Cells[1] as DataGridViewComboBoxCell;
string[] colors = { "white", "black" };
switch (dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString())
{
case "Pilot": colors = new string[] { "silver", "sage" }; break;
// case "other": add other colors
}
cbCell.DataSource = colors;
}
}
如果你的颜色类型像枚举当然你应该使用这些类型,而不是我打开和插入这里的字符串...
If your colors (and maybe even cars) are strong types like enumerators of course you should use those types instead of the strings I'm switching on and inserting here...
这篇关于DataGridView组合框,每个单元格使用不同的dataSources的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!