2012-02-06 27 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

相關問題