2014-01-20 77 views
1

在我的DataGrid中,我在第一列中使用了一個ComboBox,所以它將使用DisplayMemberValueMember概念從數據庫中獲取一些數據。現在我想從先前選擇的ComboBox中刪除值。根據條件從數據表中刪除行

填充組合框代碼如下:

dTable = getDummyTable.GetDummyTble("Dummy", "DNO", "All"); 
    dCmbData = dTable; 
    cmbDeno.DataSource = dTable; 
    cmbDeno.DisplayMember = "dyName"; 
    cmbDeno.ValueMember = "dyRemarks"; 

選擇的下一行不應該有在ComboBox一個重複的值。

我該如何做到這一點?

任何人都可以幫助我嗎?

+0

什麼UI? WPF?的WinForms? – StevieB

+0

它是C#中的一個winforms。# – ShaQue

+0

你的問題到底是什麼?標題說你想從DataTable中刪除行。問題說你想從ComboBox中刪除重複的值。評論說你想刪除先前選定的值。請定義你正在嘗試做什麼 –

回答

1

試試這個吧。你有你用來綁定所有組合的DataTable(糾正我,如果我錯了)。現在我們需要做的就是當從任何一個組合中選擇一個物品時,我們需要從其他所有組合中移除該物品)。您需要聲明一個類級字典存儲其組合此前什麼樣的價值存儲:

IDictionary<ComboBox, DataRow> _prevSelection; 

//Please don't mind if syntax is wrong worked too long in web 
comboBox.OnSelectedIndexChanged += fixItems; 

private void fixItems(object sender, EventArgs e) 
{ 
    var cbo= sender as ComboBox; 
    if(cbo==null) return; 

    var prev = _prevSelection[cbo]; 

    var row=<GET ROW FROM DATATABLE FOR CURRENT SELECTED VALUE>; 

    _prevSelection[cbo] = row; 

    UpdateOtherCombos(cbo, prev, cbo.SelectedItem.Value); 
} 

private void UpdateOtherCombos(ComboBox cbo, DataRow prev, object toRemove) 
{ 
    foreach(var gridrow in <YourGrid>.Rows) 
    { 
     var c = <FIND COMBO IN ROW>; 
     if(cbo.Id == c.Id) continue;//combo that triggered this all 
     var itemToRemove=null; 
     foreach(var item in c.Items) 
     { 
      if(item.Value == toRemove) 
      { 
       itemToRemove = item; 
       break; 
      } 
     } 

     //or you can get index of item and remove using index 
     c.Items.Remove(itemToRemove); 

     //Now add the item that was previously selected in this combo (that 
     //triggered this all) 
     c.Items.Add(new ComboBoxItem{Value = prev["ValueColumn"], 
             Text = prev ["TextColumn"]}); 
    } 
} 

這只是給你一個想法,可以幫助你找到一個最佳的解決方案,而不是遍歷所有的混合體作爲如果網格中的行太多或組合中的項太多,它會減慢速度。儘管如此,你仍然需要提供一些函數/代碼來使其工作。另外請注意,我有一段時間沒有在WinForms上工作,您需要檢查是否存在類似ComboBoxItem等等以及對它們調用的任何函數。 :)

+0

@VillageIdiot謝謝你...... – ShaQue

+0

很高興我能夠提供幫助。是我打牀前的最後一次打字。 :) – TheVillageIdiot