2012-02-06 30 views
1

我想创建一个DataGridView,其中保存配置信息。DataGridView组合框与每个单元不同的数据源

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

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

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

什么是一个好的设计来实现这种类型的用户界面?

回答

8

您可以分别设置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; 
    } 
} 

如果你的颜色(甚至汽车)是强类型,如课程的统计员,你应该使用这些类型而不是字符串我切换在这里插入...

+0

感谢您的答案,正是我需要的 – DarwinIcesurfer 2012-02-07 16:41:25

相关问题