2014-01-20 18 views
1

我有一个DataTable。不同的DataSource到DataGridViewComboBoxCell

CurrencyId |货币


0 | USD

1 |泰铢

2 |欧元

5 |卢比

6 |日元

我已经将此表绑定到DataGridViewCombobox单元。用户可以选择一种货币一次。如果用户在第一个DataGridViewRow中选择'USD',则下一行的组合框将不包含'USD'。我能得到它吗?我试过这个。

private void setCellComboBoxItems(DataGridView dataGrid, int rowIndex, int colIndex, DataTable itemsToAdd) 
    { 
     DataGridViewComboBoxCell currencycell = (DataGridViewComboBoxCell)dataGrid.Rows[rowIndex].Cells[colIndex]; 

     currencycell.DataSource = dtCurrency; 
     currencycell.ValueMember = "CurrencyId"; 
     currencycell.DisplayMember = "CurrencyShortName"; 
    } 

我无法修改数据源属性。我怎么才能得到它?谢谢。

+0

维护TMP数据表并从数据表中删除所选择的值和TMP数据表绑定到''currencycell.DataSource – Damith

+0

当我在TMP数据表结合currencycell.DataSource ,上述单元格的选定值将消失。 @Damith – Zan

回答

0

有一个数据源的副本,您可以在其中删除用作显示数据源的选定值。

订阅DataGridView.EditingControlShowing事件,然后从下面的编辑控件中获取组合框,并将其数据源设置为数据源的副本。

示例代码:

void myDGV_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e) 
    { 
     ComboBox comboBox = e.Control as ComboBox; 
     if (comboBox != null) 
     { 
      comboBox.DataSource = displayDataSource; 
     } 
    } 
相关问题