0

私はよく見回してきましたが、この問題がどこから来ているのかわかりません。何が起こっているかというと、BindingSource、SqlDataAdapter、SqlCommandBuilder、および DataTable を使用する datagridview があるということです。datagridview には、単純な選択クエリ (MSSQL Server DB から 1 つのテーブルのみを使用) が取り込まれます。このテーブルの内容を編集できるようにしたいです。現在、編集は機能していますが、本来の方法ではありません。セルを編集し、Enter キーを押して、変更をデータベースにコミットできると思います。実際には、2 番目のセルの編集が完了するまで変更が反映されません。私がここで見落としていることを知っている人はいますか?ありがとう!

.cs ファイル内の関連コードは次のとおりです。

public partial class CheckIn : Form
{

    private BindingSource searchDGVBindingSource = new BindingSource();
    private SqlDataAdapter searchDGVDataAdapter;
    private SqlCommandBuilder searchDGVSqlCommandBuilder;
    private DataTable  searchDGVDataTable;


    public CheckIn()
    {
        InitializeComponent();
        this.Load += new System.EventHandler(CheckIn_Load);
    }

    private void CheckIn_Load(object sender, EventArgs e)
    {
        searchDGV.DataSource = searchDGVBindingSource;
        searchDGVDataAdapter = new SqlDataAdapter(selectCommand, connectionString);
        searchDGVSqlCommandBuilder = new SqlCommandBuilder(searchDGVDataAdapter);
        searchDGVDataTable = new DataTable();
        searchDGVDataTable.Locale = System.Globalization.CultureInfo.InvariantCulture;
        searchDGVDataAdapter.Fill(searchDGVDataTable);
        searchDGVBindingSource.DataSource = searchDGVDataTable;

        searchDGV.AutoResizeColumns();
    }

    private void searchGridView_RowEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        searchDGVDataAdapter.Update(searchDGVDataTable);
    }
}

.Designer.cs ファイルからの関連コードは次のとおりです。

        // 
        // searchDGV
        // 
        this.searchDGV.AllowUserToAddRows = false;
        this.searchDGV.AllowUserToDeleteRows = false;
        this.searchDGV.BackgroundColor = System.Drawing.Color.White;
        this.searchDGV.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.searchDGV.Location = new System.Drawing.Point(10, 250);
        this.searchDGV.MultiSelect = false;
        this.searchDGV.Name = "searchDGV";
        this.searchDGV.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
        this.searchDGV.Size = new System.Drawing.Size(619, 150);
        this.searchDGV.TabIndex = 7;
        this.searchDGV.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.searchGridView_CellClick);
        this.searchDGV.CellEndEdit += new System.Windows.Forms.DataGridViewCellEventHandler(this.searchGridView_RowEndEdit);
4

1 に答える 1

2

searchDGVBindingSource.EndEdit()イベントハンドラーを呼び出す必要があり、それは機能します:

private void searchGridView_RowEndEdit(object sender, DataGridViewCellEventArgs e)
{
    searchDGVBindingSource.EndEdit();
    searchDGVDataAdapter.Update(searchDGVDataTable);
}

詳細については、ドキュメントを確認しBindingSource.EndEdit()てください。

于 2012-02-24T20:36:03.073 に答える