1

问题:

我正在尝试通过将一行信息直接添加到网格中来更新数据库,捕获数据并将其提交到数据库。

好的,所以我在 UltraGrid 中插入一行,关闭我的应用程序并重新启动它...有时它会提交行,有时如果我重新启动应用程序,所有新条目(非常旧的条目)都会被删除。

这是我的代码:

捕获对新插入行的引用:

    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();
            }
        }
    } 

If key = Enter Show The New Entered ID value 通过 MainActionsInsert() 添加到 DB // 下面

    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 执行插入命令

最后

    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);
    }
4

0 回答 0