简体   繁体   English

使用C#将新添加的datagridview行更新到oledb数据库

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

I have a datagridview and an Add and Save button. 我有一个datagridview和一个添加保存按钮。 Each time I add row to the datagridview with Add button and at the end save all rows to the database by Save button. 每次我使用“添加”按钮将行添加到datagridview中,最后通过“保存”按钮将所有行保存到数据库中。

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. 我想再次向同一 datagriview添加/很少几行,然后保存到数据库, 但是在将新项目保存到数据库时,它将再次获取整个datagridview行并保存到数据库。

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. 我的datagridview总是在最后向数据库添加/插入数据。

I am using below code for saving datagridvew rows to database : 我正在使用以下代码将datagridvew行保存到数据库:

                    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. 如果您没有我在下面说的话,我会给您举一个例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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