2012-02-24 82 views
0

我一直在寻找很多,我无法弄清楚这个问题来自哪里。发生什么事是我有一个使用BindingSource,SqlDataAdapter,SqlCommandBuilder和DataTable的datagridview。 datagridview填充了一个简单的select查询(它只使用MSSQL Server DB中的一个表)。我希望能够编辑此表中的内容。现在,编辑工作,但不是它应该的方式。我想我可以编辑一个单元格,按回车键,并将更改提交给数据库。实际发生的情况是,直到完成编辑第二个单元格为止,这些更改才会出现。任何人都知道我在这里忽略了什么?谢谢!DataGridView和SqlDataAdapter没有正确更新

这里是在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); 
+0

任何你有'Ce​​llEndEdit'事件被一个名为'searchGridView_RowEndEdit'的方法处理的原因? – 2012-02-24 19:21:04

+0

我只是没有将searchGridView_RowEndEdit方法重命名为更合适的东西。我之所以这样命名,是因为我正在尝试一种不同的方法。 – mjb2424 2012-02-24 20:11:38

+0

您是否验证了您正在执行的更改实际上是有效的? – 2012-02-24 20:30:24

回答

2

你需要调用searchDGVBindingSource.EndEdit()在事件处理程序,它会工作:

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

检查documentationBindingSource.EndEdit()了解更多信息。

+0

这个伎俩!我花了相当长的时间试图追查下来,谢谢! – mjb2424 2012-02-25 01:54:39