0

I have a form where I can update my data in the datagridview and it will save a log.

My problem is that every time I saved my data it insert all the data in the datagrid.

How will i save data that will only save if i made changes to the datagridview? is that possible?

Here is a snapshot of my UI:

Datagridviewsample

And my code code that saves the data:

//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();
    }        
}
dbc
  • 104,963
  • 20
  • 228
  • 340
ommpi5991
  • 23
  • 4
  • Does this answer your question? [DataGridView Event to Catch When Cell Value Has Been Changed by User](https://stackoverflow.com/q/19537784/3744182). Or, alternatively, if you were to bind to a `DataTable`, does this answer your question? [Return only the changed rows of a datagridview?](https://stackoverflow.com/q/25060745/3744182) – dbc Nov 28 '21 at 21:50

1 Answers1

0

An easy way is to use a DataTable for the DataGridView DataSource working with DataTable method GetChanges.

To see an examples see the following GitHub repository. The core code is in the following class using extension methods. Note that the code presented is not a direct drop-in and use but provides the basics for you to adapt to your task.

Karen Payne
  • 4,341
  • 2
  • 14
  • 31