2012-07-17 136 views
0

對於產品有1個datagridview,對於類別有1個組合框,我已將組合框的選定值設置爲CategoryID。具有選定值和自動完成功能的組合框

當我在組合框中鍵入類別名稱的第一個字母,然後按下輸入名稱在組合框內完成,但datagridview中的相關字段不會更改,直到我單擊組合外。

請問有沒有辦法讓按Enter鍵在datagridview中執行更改,以便我可以直接在保存按鈕單擊時保存修改。

+0

你如何將你的類別綁定到組合框並更新gridview?我今天做了同樣的事情,它工作 – codingbiz 2012-07-17 22:36:10

+0

@tcoder,我通過databindingsource和databindingNavigator將產品(datagrid)和數據綁定源分類(組合)使用L2SQL。所有工作都很好,但在將數據保存到數據庫(保存bindingSourceNavigator的按鈕)之前,無法使用「Enter」鍵同時更新兩者。 – 2012-07-17 23:39:44

+0

我實際上有一箇中心方法'fillGrid()',可以從任何點調用,但我沒有使用bindingsource。在fillGrid中,我只有'mygridview.DataSource = MyClass.GetSomeData()' – codingbiz 2012-07-17 23:54:57

回答

0

我可以simultanuously在按下「Enter」鍵連擊自動完成關鍵,通過下一個事件更新產品的BindingSource中的categoryID填寫: cboCategories_ SelectedValueChanged如下:

private void cboCategories_SelectedValueChanged(object sender, EventArgs e) 
{ 
    int val = Convert.ToInt32(cboCategories.SelectedValue); 
    ModifGrid(val); 
} 
private void ModifGrid(int ModifiedValue) 
{ 
    if (Convert.ToInt32(productBindingSource.Count)>0) 
    { 
     ((Product)productBindingSource.Current).CategoryID = ModifiedValue; 
    } 
} 

點擊保存按鈕在bindingsourceNavigator更改保存到數據庫後。

1

這樣在GridView可以從任何來源通過調用FillGrid()

private void FilterComboBox_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     string selText = FilterComboBox.SelectedValue.ToString();   
     FillGrid(selText); 
    } 

    private void FillGrid(string filterValue = "0") 
    { 
     //GetDefaultValues(if filtervalue = 0) 
     //else GetValues(based On Selected category) 
     //Bind Values to Grid 
    } 
+0

在這種情況下,我們不使用組合過濾datagrid行,僅使用組合在產品表中更改類別。 – 2012-07-18 10:32:00