我一直在尋找很多,我無法弄清楚這個問題來自哪裏。發生什麼事是我有一個使用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);
任何你有'CellEndEdit'事件被一個名爲'searchGridView_RowEndEdit'的方法處理的原因? – 2012-02-24 19:21:04
我只是沒有將searchGridView_RowEndEdit方法重命名爲更合適的東西。我之所以這樣命名,是因爲我正在嘗試一種不同的方法。 – mjb2424 2012-02-24 20:11:38
您是否驗證了您正在執行的更改實際上是有效的? – 2012-02-24 20:30:24