【发布时间】: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();
}
}
【问题讨论】:
-
这能回答你的问题吗? DataGridView Event to Catch When Cell Value Has Been Changed by User。或者,或者,如果您要绑定到
DataTable,这是否回答了您的问题? Return only the changed rows of a datagridview?
标签: c# winforms datagridview