簡體   English   中英

聚焦時不會保存DataGridView ComboBox

[英]DataGridView ComboBox doesn't save when focus

在DataGridView ComboBox中選擇一個值並單擊bindingnavigator中的保存按鈕后,數據不會在數據庫中獲得更新。 用戶必須失去焦點才能更新數據。 有辦法解決這個問題嗎?

奇怪的。 我最近做了這個,它就像一個魅力。 在我的應用程序中,當gridview處於“編輯”模式(只讀false)時,當您選擇單元格時,它將變為組合框,而當您離開該單元格時,它將表現為文本框。 這是我所做的

void dgUpdateItems_CellEnter(object sender, DataGridViewCellEventArgs e)
{
    DataGridView dg = (DataGridView)sender;
    if (e.ColumnIndex == dg.Columns["ItemCategory"].Index)
    {
        if (e.ColumnIndex == e.RowIndex)
        {
            dg[e.ColumnIndex, e.RowIndex].ReadOnly = true;
            return;
        }
        DataGridViewComboBoxCell cmbCell = new DataGridViewComboBoxCell();
        ComboUpdate(cmbCell);
        cmbCell.Value = ((DataGridView)sender)[e.ColumnIndex, e.RowIndex].Value.ToString();
        ((DataGridView)sender)[e.ColumnIndex, e.RowIndex] = cmbCell;
    }
}

void dgUpdateItems_CellLeave(object sender, DataGridViewCellEventArgs e)
{
    DataGridView dg = (DataGridView)sender;
    if (e.ColumnIndex == dg.Columns["ItemCategory"].Index)
    {
        if (e.ColumnIndex == e.RowIndex)
            return;

        string str = dg[e.ColumnIndex, e.RowIndex].Value.ToString();
        DataGridViewComboBoxCell cmb = (DataGridViewComboBoxCell)dg[e.ColumnIndex, e.RowIndex];
        string val = cmb.Value.ToString();

        dg[e.ColumnIndex, e.RowIndex] = new DataGridViewTextBoxCell();
        dg[e.ColumnIndex, e.RowIndex].Value = val;

如果不理解,這是我代碼的一部分,請告訴我。 這是檢查鏈接。 這可能會有所幫助。 編輯模式下DatagridView中的ComboBox

抱歉,忘了告訴您最重要的事情,即使重點突出,它仍然有效。 如果您正在尋找其他東西,請在我的頭上扔一塊石頭。 希望能幫助到你。

像這樣保存之前,請嘗試調用UpdateSource

ComboBox c = Keyboard.FocusedElement as ComboBox;
if ((c != null) && (c.GetBindingExpression(ComboBox.TextProperty) != null))
  c.GetBindingExpression(ComboBox.TextProperty).UpdateSource();

高溫超導

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM