2012-05-07 66 views
0

我在因特網上搜索了幾天如何創建可用於編輯值的數據綁定表單,但沒有運氣。如果有人能幫助我,我會很感激!將數據綁定WinForm數據插入到數據庫中

讀值完美,但更新/插入不能。

FormLoad:

newRow = issueTable.NewRow(); 
newRow["ID"] = Id; 
issueTable.Rows.Add(newRow); 
txtName.DataBindings.Add("Text", issueTable, "Name"); 

節省:

SqlCommandBuilder build = new SqlCommandBuilder(adaptor); 
adaptor.InsertCommand = build.GetInsertCommand(); 
adaptor.Update(issueTable); 

我的問題是,在SQL表: 「ID」 得到保存,但 「名稱」 待在空。對我來說奇怪的是,如果我監視issueTable.Rows [0] [「Name」],它將返回表單上的值。

謝謝!

回答

1

問題是我想同時插入和編輯。我沒有意識到如果一行處於插入狀態,它會阻止更新。

解決方案:

負載:

newRow = issueTable.NewRow(); 
newRow["ID"] = Id; 
issueTable.Rows.Add(newRow); 
txtName.DataBindings.Add("Text", issueTable, "Name"); 
newRow.AcceptChanges(); //Remove the Insert State, to accept modifications 
isNew = true; 

節省:

if (isNew) 
{ 
    newRow.SetAdded(); //Add the Insert State back 
    isNew = false; 
} 

SqlCommandBuilder build = new SqlCommandBuilder(adaptor); 
adaptor.InsertCommand = build.GetInsertCommand(); 
adaptor.UpdateCommand = build.GetUpdateCommand(); 
adaptor.Update(issueTable);