2012-02-24 16 views
0

私は多くのことを見てきましたが、この問題の原因がわかりません。私は、BindingSource、SqlDataAdapter、SqlCommandBuilder、およびDataTableを使ってdatagridviewを持っているということが起こっています。 datagridviewには、単純なselectクエリ(MSSQL Server DBの1つのテーブルのみを使用)が設定されています。私はこの表のものを編集できるようにしたい。今は編集作業が必要です。私は、セルを編集してEnterキーを押し、データベースにコミットされた変更を行うことができると思います。実際に何が起こるかは、2番目のセルの編集が終了するまで変更が表示されないことです。誰も私がここで見落としているものを知っていますか?ありがとう!ここでDataGridViewとSqlDataAdapterが正しく更新されない

は.csファイルのファイルに関連するコードです:あなたはあなたのイベントでsearchDGVBindingSource.EndEdit()を呼び出す必要が

 // 
     // 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); 
+0

'CellEndEdit'イベントが' searchGridView_RowEndEdit'というメソッドで処理されている理由は何ですか? –

+0

私は、searchGridView_RowEndEditメソッドの名前をより適切なものに変更していません。私はそれに名前を付けました。なぜなら私は別のアプローチを試みていたからです。 – mjb2424

+0

実行している変更が実際に有効であることを確認しましたか? –

答えて

2

:ここ

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ファイルから該当するコードですハンドラとそれが動作します:ため

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

チェックdocumentation詳細については、を参照してください。

+0

それはトリックでした!私はこれを追跡しようとかなりの時間を費やしました、ありがとう! – mjb2424

関連する問題