在已加载已保存数据的GridView中保存新数据. [英] Saving New Data in a GridView with Saved data already loaded.

查看:61
本文介绍了在已加载已保存数据的GridView中保存新数据.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataGridView,我正在使用Web Service将数据加载到其中.我想要做的是在已保存并加载的数据下添加另一行,然后仅保存该行.

我可以让它很好地加载新数据,如果没有数据,我可以将其保存起来.但是当网格中已经有数据时,当我保存新数据时,它将保存另一组已保存的数据以及我的新数据.因此,如果有2个保存的行,而我现在又添加了1行,那么有3个保存的行记录了5条记录,而不是1条总共保存了3条记录.

我需要弄清楚的是如何只插入一条新记录,并避免重复已存在的记录.我想避免使用按钮添加新项并清除数据,因为这会引起问题.

这是我保存行的代码

I have a DataGridView I am loading Data into with my Web Service. What I want to be able to do is add another line under the already saved and loaded data and then save only that one line.

I can get it to load new data just fine and if there is no data I can get it to save fine. But when I save new data when there is already data in the Grid, it saves another set of the saved data plus my new data. So if there were 2 saved rows and I added 1 row now there are 3 rows that saved making 5 records rather than 1 making a total of 3 saved records.

What I need to figure out is how do I insert a new record only and keep from duplicating the records that already exist. I want to avoid using a button to add new items and clear the data as it is causing issues.

Here is my code for Saving the rows

Sub InsertAir()
Dim ROW_ID As Integer
 Dim TEST_ID As String = txtTestID.Text
        Dim AIRTYPE As String
        Dim AIR_PRESS As String
        Dim RESULTS As String
        Dim PASS As String
        Dim METHOD As String
        Dim NOTES As String
        Cursor = Cursors.No
        Dim x As Integer
        For x = 0 To dgAir.Rows.Count - 2
            GetRowID()
            ROW_ID = Val(txtRowID.Text)
            AIRTYPE = dgAir.Rows(x).Cells(0).Value
            AIR_PRESS = dgAir.Rows(x).Cells(1).Value
            RESULTS = dgAir.Rows(x).Cells(2).Value
            PASS = dgAir.Rows(x).Cells(3).Value
            METHOD = dgAir.Rows(x).Cells(4).Value

            Dim sql As String = "INSERT INTO TEST_LAB_TAB_AIR (ROW_ID, "
            sql = sql & "                              TEST_ID, "
            sql = sql & "                              TYPE, "
            sql = sql & "                              PRESSURE, "
            sql = sql & "                              RESULT, "
            sql = sql & "                              PASS_FAIL, "
            sql = sql & "                              TEST_METHOD) VALUES (" & ROW_ID & "," & TEST_ID & "," & _
                    "'" & AIRTYPE & "','" & AIR_PRESS & "','" & RESULTS & "','" & PASS & "','" & _
                    "" & METHOD & "')"
            Dim s As String
            Dim TestLab As New TestLab.ServiceSoapClient("ServiceSoap")
            s = TestLab.InsertTabs(sql)
        Next
End Sub



这是我将数据加载到网格中的位置.



And here is where I am loading the Data into the Grid.

Sub GetAir()
        Dim i As Integer = CInt(gv.SelectedCells(0).Value.ToString)
        On Error GoTo 10

        Dim ds As New DataSet
        Dim dt As New DataTable
        Dim Row As DataRow

        Dim TestLab As New TestLab.ServiceSoapClient("ServiceSoap")
        ds = TestLab.GetAir(i)
        Row = ds.Tables("ImageFile").Rows(0)
10:
        If Err.Number <> 0 Then
            Err.Clear()
            dt.Clear()
            ds.Clear()
        End If
        dt = ds.Tables(0)
        System.Web.HttpContext.Current.Session("MyData") = dt
        dgAir.DataSource = dt

    End Sub

任何帮助将不胜感激!!!!!!

Any help would be appreciated!!!!!!
Thanks

推荐答案

我在数据库中有一个名为Row_id的主键,但保存时,它正在为表中的每一行获取一个新的Row_ID.我需要以某种方式捕获它已经存在,然后跳过那些记录.有关如何执行此操作的任何建议?我知道您可以执行If Statment和For循环,但不确定如何编码.
I have a Primary key in the Database called Row_id, BUT, When I save it is getting a New Row_ID for each row in the table. I need to somehow capture that it already exists and then skip over those records. Any suggestions on how to do this? I know you can do an If Statment and For loops but I am not sure on how to code it.


最后弄清楚了.这是我的代码

Finally figured it out. here is my code

For x = 0 To dgAir.Rows.Count - 2
            Dim w As String = dgAir.Rows(x).Cells("AirID").Value.ToString
            If w = "" Then
                GetRowID()
                AIRTYPE = dgAir.Rows(x).Cells("AirType").Value
                AIR_PRESS = dgAir.Rows(x).Cells("AirPress").Value
                RESULTS = dgAir.Rows(x).Cells("AirResult").Value
                PASS = dgAir.Rows(x).Cells("AirPass").Value
                METHOD = dgAir.Rows(x).Cells("AirMethod").Value
                NOTES = dgAir.Rows(x).Cells("AirNotes").Value.ToString
                ROW_ID = Val(txtRowID.Text)

                Dim sql As String = "INSERT INTO TEST_LAB_TAB_AIR (ROW_ID, "
                sql = sql & "                              TEST_ID, "
                sql = sql & "                              TYPE, "
                sql = sql & "                              PRESSURE, "
                sql = sql & "                              RESULT, "
                sql = sql & "                              PASS_FAIL, "
                sql = sql & "                              TEST_METHOD, "
                sql = sql & "                              NOTES) VALUES (" & ROW_ID & "," & TEST_ID & "," & _
                        "'" & AIRTYPE & "','" & AIR_PRESS & "','" & RESULTS & "','" & PASS & "','" & _
                        "" & METHOD & "','" & NOTES & "')"
                Dim s As String
                Dim TestLab As New TestLab.ServiceSoapClient("ServiceSoap")
                s = TestLab.InsertTabs(sql)
            End If
        Next



我最终不得不将代码放入if语句中,该语句查找Row_ID,如果为空,则将其添加,但是如果该项目存在,则跳至下一个项目.

希望这有助于解决问题,上班真的很痛苦!



I ended up having to put my code into an if statment that looked for the Row_ID and if it was blank yet then it added it but if the item existed, then it skipped to the next item.

Hope this helps somone out, was a real pain to get to work!!


这篇关于在已加载已保存数据的GridView中保存新数据.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆