2

我正在尝试创建一个包含配置信息的 DataGridView。

可用值可以根据不同列中的值更改列中每一行的值,因此我无法将单个数据源附加到组合框列。例如:如果您选择汽车,则可用颜色应仅限于该型号可用的颜色。

Car                 ColorsAvailable
Camry               {white,black}
CRV                 {white,black}
Pilot               {silver,sage}

考虑使用 dataGridView 的原因是操作员可以为其他汽车添加行。

实现这种类型的 UI 有什么好的设计?

4

1 回答 1

10

您可以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;
    }
}

如果您的颜色(甚至可能是汽车)是像枚举器这样的强类型,那么您当然应该使用这些类型而不是我正在打开并在此处插入的字符串...

于 2012-02-07T00:56:36.343 回答