【问题标题】:How can I create a history log every time I update a datagridview每次更新 datagridview 时如何创建历史日志
【发布时间】:2021-11-25 03:27:53
【问题描述】:

我有一个表单,我可以在其中更新 datagridview 中的数据,它会保存一个日志。

我的问题是每次我保存数据时都会在数据网格中插入所有数据。

我将如何保存仅在我对 datagridview 进行更改时才会保存的数据?这可能吗?

这是我的 UI 快照:

还有我保存数据的代码:

//this is the formula for inserting history logs
MySqlConnection conn = new MySqlConnection("Data Source=" + clsSQLcon.sqlServerName + ";port=" + clsSQLcon.sqlPort + ";Initial Catalog =" + clsSQLcon.sqlDatabaseName + ";user id =" + clsSQLcon.sqlUserName + ";password =" + clsSQLcon.sqlPassword + "");
conn.Open();
string query = "INSERT INTO tblhistorylogs(DATE_UPDATED,NAME,LOGS) VALUES" +
    "(@Date,@Name,@HistoryLogs)";
MySqlCommand command = new MySqlCommand(query, conn);

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
    if (txtPRSSearch.Text.Length <= 3)
    { }
    else
    {
        command.Parameters.AddWithValue("@Date", DateTime.Today.ToString("yyyy-MM-dd"));
        command.Parameters.AddWithValue("@Name", "Updated by: " + LoginForm.SetValueForText1 +"\n("+ cbxPRSctrl.Text + "-" + txtPRSSearch.Text +")");
        command.Parameters.AddWithValue("@HistoryLogs",
      
          "STATUS: " + Convert.ToString(dataGridView1.Rows[i].Cells[0].Value) + "\n" +
          "ITEM NAME: " + Convert.ToString(dataGridView1.Rows[i].Cells[5].Value) + "\n" +
          "DATE RECEIVED: " + Convert.ToString(dataGridView2.Rows[i].Cells[0].Value) + "\n" +
          "RECEIVED BY: " + Convert.ToString(dataGridView3.Rows[i].Cells[0].Value) + "\n" +
          "SUPPLIER: " + Convert.ToString(dataGridView3.Rows[i].Cells[1].Value) + "\n" +
          "PO/PCF/PIS: " + Convert.ToString(dataGridView3.Rows[i].Cells[2].Value) + "\n" +
          "UNIT COST " + Convert.ToString(dataGridView3.Rows[i].Cells[3].Value) + "\n" +
          "AMOUNT: " + Convert.ToString(dataGridView3.Rows[i].Cells[4].Value) + "\n" +
          "REF/CV/JV: " + Convert.ToString(dataGridView3.Rows[i].Cells[5].Value) + "\n" +
          "DATEPAID: " + Convert.ToString(dataGridView3.Rows[i].Cells[6].Value) + "\n" +
          "INCHARGE: " + Convert.ToString(dataGridView3.Rows[i].Cells[7].Value) + "\n" +
          "TARGET DATE: " + Convert.ToString(dataGridView3.Rows[i].Cells[8].Value));

        command.ExecuteNonQuery();
        command.Parameters.Clear();
    }        
}

【问题讨论】:

标签: c# winforms datagridview


【解决方案1】:

一个简单的方法是使用DataGridView DataSource 的DataTable 和DataTable 方法GetChanges

要查看示例,请参阅以下 GitHub repository。核心代码在下面class 使用扩展方法。请注意,提供的代码不是直接插入和使用的,而是为您提供适应您的任务的基础知识。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多