问题:
我试图通过直接在网格中添加一行信息来更新数据库,捕获数据并将其提交给数据库。
好的,所以我在UltraGrid中插入一行,关闭我的应用程序并重新启动它...有时它会提交行和其他时间如果我重新启动应用程序除了2(非常旧的条目)之外的所有新条目都被删除。
这是我的代码:
抓住对新插入行的引用:
private void ultraGrid1_BeforeRowDeactivate(object sender, CancelEventArgs e)
{
if (!first) //Ignore this step if application has just started
{
UltraGrid g = (UltraGrid)(sender);
r = g.ActiveRow;
ultraGrid1.Rows[g.ActiveRow.Index].Cells["Is Closed"].Value = false;
}
}
测试并输入密钥
private void ultraGrid1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)(13)) //Test for ENTER key
{
if (r.Band.Index == 0)
{
MessageBox.Show(r.Cells["ID"].Text);
string Action = (string)r.Cells[1].Value;
bool isChecked = (bool)r.Cells[2].Value;
MainActionsInsert(Action, isChecked);
UpdateTables();
}
}
}
如果key = Enter显示新输入的ID值 通过MainActionsInsert()//在
下面将它添加到数据库中 private void MainActionsInsert(string Action, bool Checked)
{
OleDbCommand Command = new OleDbCommand("INSERT INTO MainActions Values (ID, Action, BoolValue)", DataBaseConnection);
//Add Parameters
Command.Parameters.AddWithValue("ID", GenerateID());
Command.Parameters.AddWithValue("Action", Action);
Command.Parameters.AddWithValue("BoolValue",Checked);
//Add Command
MainActionsAdapter.InsertCommand = Command;
//Execute Agains DataBase
Command.ExecuteNonQuery();
//Accept Changes
}
通过Command.ExecuteNonQuery执行Insert命令
最后
private void UpdateTables()
{
ultraGrid1.UpdateData();
ultraGrid2.UpdateData();
ultraGrid3.UpdateData();
//
minutesDataSet.AcceptChanges();
MainActionsAdapter.Update(minutesDataSet.MainActions);
SubActionsAdapter.Update(minutesDataSet.SubActions);
SubActionsNotesAdapter.Update(minutesDataSet.SubActionNotes);
//
MainActionsAdapter.Fill(minutesDataSet.MainActions);
SubActionsAdapter.Fill(minutesDataSet.SubActions);
SubActionsNotesAdapter.Fill(minutesDataSet.SubActionNotes);
}