简体   繁体   中英

Update newly added rows of datagridview to oledb database using c#

I have a datagridview and an Add and Save button. Each time I add row to the datagridview with Add button and at the end save all rows to the database by Save button.

I want add/few few rows again to the same datagriview and then save to the database but while saving new items to the database it takes whole datagridview rows again and saves to database.

I wanted to save only new added data or edited data. Is there any way to set focus to the newly added rows or edited rows ? and at the end only saves the newly added or edited rows?

My datagridview always adds/inserts data to database at the end.

I am using below code for saving datagridvew rows to database :

                    int chkLoop;

                    chkLoop = dgvOrderList.Rows.Count;

                    MessageBox.Show(chkLoop.ToString());
                    foreach (DataGridViewRow row in dgvOrderList.Rows)
                    {
                        //MessageBox.Show(chkLoop.ToString());
                        //MessageBox.Show(row.co.ToString());
                        if (chkLoop != 1)
                        {
                            comm = new OleDbCommand("INSERT INTO Orders_Description (ItemCode, Quantity, Amount, OrderId, CreatedOn) VALUES ('" + Convert.ToInt32(row.Cells["Code"].Value) + "', '" + Convert.ToInt32(row.Cells["Quantity"].Value) + "', '" + Convert.ToInt32(row.Cells["Amount"].Value) + "', '" + Convert.ToInt32(txtOrderNo.Text) + "', '" + DateTime.Now + "')", conn);
                            MessageBox.Show(comm.CommandText);

                            comm.ExecuteNonQuery();
                            chkLoop = chkLoop - 1;

                        }
                    }

I'm lazy to think....... so you can add additional column with checkbox and whenever you add a new row the default value for the checkbox become selected and then when you are saving just check of that specific checkbox is checked or not. if not checked it means it's not new row then continue to next iteration.

If you didn't got what I said comment below i will give you an example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM