Dar*_*fer 1 combobox datagridview datagridviewcombobox
我正在尝试创建一个包含配置信息的DataGridView.
可以根据不同列中的值为列中的每一行更改可用值,因此我无法将单个数据源附加到comboBox列.例如:如果选择汽车,则可用颜色应限制为该模型可用的颜色.
Car ColorsAvailable
Camry {white,black}
CRV {white,black}
Pilot {silver,sage}
Run Code Online (Sandbox Code Playgroud)
考虑dataGridView的原因是操作员可以为其他汽车添加行.
实现这种类型的UI有什么好的设计?
您可以DataSource在每个上单独设置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;
}
}
Run Code Online (Sandbox Code Playgroud)
如果您的颜色(甚至可能是汽车)是类似枚举器的强类型,您当然应该使用这些类型而不是我正在打开并插入此处的字符串......
| 归档时间: |
|
| 查看次数: |
6273 次 |
| 最近记录: |