簡體   English   中英

在DataGridView的CellValueChanged事件上更新ADO.NET TableAdapter

[英]ADO.NET TableAdapter Updating on CellValueChanged Event for a DataGridView

我有一個DataGridView,其中包括數據庫中布爾變量的復選框列。

當我單擊此復選框時,它將觸發以下事件。

在uiPersonGridView_CellValueChanged事件內部,我在Persons數據表(它是DataGridView的數據源)上調用了數據表適配器更新方法。

更新第二次工作正常,但仍不會更新當前行數據嗎?

例如,如果我有兩行,則在第一行中打勾。 更新返回0行更新為int i,我檢查數據庫,但沒有任何更改。 但是,如果我在第二行復選框上打鈎,更新將返回更新為int i的1行-我檢查數據庫,並且第一條記錄已更改,而不是第二條記錄已更改?

我怎樣才能使它起作用,以便更新適用於初始更改以及此后的更改?

    private void uiPersonGridView_CurrentCellDirtyStateChanged(object sender, EventArgs e)
    {
        if (uiPersonGridView.IsCurrentCellDirty)
        {
            uiPersonGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
        }
    }

    private void uiPersonGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
    {
        if (uiPersonGridView.DataSource == null)
            return;

        if (e.RowIndex == -1)
            return;

        int i = _personAdapter.Update(_person);
    }

為了使此工作有效,我必須做的是將數據行狀態設置為已修改,這已經完成了我需要做的事情。

private void uiPersonGridView_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
    if (uiPersonGridView.IsCurrentCellDirty)
    {
        uiPersonGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
           DataRowView drv = uiPersonGridView.CurrentRow.DataBoundItem as DataRowView;
            drv.Row.SetModified();
    }
} 

暫無
暫無

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

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