我一直在环顾四周,我无法弄清楚这个问题来自哪里。发生的事情是我有一个使用 BindingSource、SqlDataAdapter、SqlCommandBuilder 和 DataTable 的 datagridview。datagridview 填充了一个简单的选择查询(仅使用 MSSQL Server DB 中的一个表)。我希望能够编辑此表中的内容。现在,编辑工作,但不是它应该的方式。我认为我可以编辑一个单元格,按回车键,并将更改提交到数据库。实际发生的情况是,在我完成第二个单元格的编辑之前,这些更改不会出现。有人知道我在这里俯瞰什么吗?谢谢!
以下是 .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);