2014-01-20 39 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; 
     } 
    } 
相關問題