【问题标题】:DataGridView comboBox with different dataSources for each cellDataGridView 组合框,每个单元格具有不同的数据源
【发布时间】:2012-02-28 02:00:02
【问题描述】:

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

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

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

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

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

【问题讨论】:

    标签: datagridview combobox datagridviewcombobox


    【解决方案1】:

    您可以在每个DataGridViewComboBoxCell上分别设置DataSource

    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;
        }
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-06
      • 1970-01-01
      • 2013-09-10
      • 2011-05-19
      • 1970-01-01
      • 2018-09-21
      • 1970-01-01
      相关资源
      最近更新 更多