如何在vb.net中删除数据行gridview?

时间:2017-06-18 13:37:09

标签: html sql vb.net

我想从数据库中使用查询sql后删除数据gridview的行 我使用这段代码

Try
        Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("en-US")

        Dim strConn As String
        strConn = "DRIVER=PostgreSQL ANSI;SERVER=127.0.0.1;PORT=5432;UID=postgres;Password=azerty79;DATABASE=" & ComboBox2.Text & ";pwd=postgres;ReadOnly=0"
        'strConn = "DRIVER={PostgreSQL ANSI};UID=postgres;pwd=postgres;LowerCaseIdentifier=0;UseServerSidePrepare=0;ByteaAsLongVarBinary=0;"

        'strConn = "dsn=test;uid=postgres;pwd=postgres"



        Dim cnDb As OdbcConnection
        Dim dsDB As New DataSet
        Dim adDb As OdbcDataAdapter
        Dim cbDb As OdbcCommandBuilder
        Dim cmd As OdbcCommand
        Dim cmd1 As OdbcCommand
        Dim adDb1 As OdbcDataAdapter
        Dim dsDB1 As New DataSet
        Dim dt As New DataTable
        Dim pic As New PictureBox




        cnDb = New OdbcConnection(strConn)
        cnDb.Open()
        dsDB = New DataSet
        adDb = New OdbcDataAdapter
        cbDb = New OdbcCommandBuilder(adDb)


        'cmd.Parameters.Add("@id", OdbcType.NVarChar, "1")



        For i = 1 To 3


            ' Create the SelectCommand.

            cmd = New OdbcCommand("select b.id_detail_polygon,b.mappe_3,b.type_d_affaire,b.consistance,b.nbr_borne ,num_bornes,a.x,a.y,b.superficie_cs  from point a right join (select (dp).path[1] d1,(dp).path[2] d2,(dp).geom d3 from (select st_dumppoints(geom) dp from polygon where id_detail_polygon ='" & i & "')a)dptable on st_equals(a.geom,dptable.d3),polygon b where id_detail_polygon='" & i & "'", cnDb) ' & _
            '"WHERE id = ? ", cnDb)
            cmd1 = New OdbcCommand("SELECT num_bornes,x,y FROM point RIGHT JOIN  (SELECT (dp).path[1] As ringID,(dp).path[2] As pointID,(dp).geom ptgeom FROM (SELECT st_dumppoints(geom) dp FROM polygon WHERE id_detail_polygon='" & i & "' ) a) dptable ON  ST_Equals(point.geom, dptable.ptgeom) ORDER BY dptable.pointID;", cnDb)
            'cmd.Parameters.Add("@id", OdbcType.NVarChar, "1")


            adDb = New OdbcDataAdapter(cmd)
            adDb1 = New OdbcDataAdapter(cmd1)



            adDb.Fill(dsDB, ComboBox1.Text)
            adDb1.Fill(dsDB1, ComboBox1.Text)
            DataGridView2.DataSource = dsDB
            DataGridView3.DataSource = dsDB1



            DataGridView2.DataMember = ComboBox1.Text
            DataGridView3.DataMember = ComboBox1.Text
            DataGridView2.DataSource = dsDB.DefaultViewManager
            DataGridView3.DataSource = dsDB1.DefaultViewManager





            Dim StrExport As String = ""



            For Each R As DataGridViewRow In DataGridView3.Rows

                For Each C As DataGridViewCell In R.Cells
                    If Not C.Value Is Nothing Then
                        StrExport &= C.Value.ToString & " "
                    Else

                    End If
                Next
                StrExport = StrExport.Substring(0, StrExport.Length - 1)
                StrExport &= Environment.NewLine
            Next














            Dim fName As String = ""
            SaveFileDialog1.InitialDirectory = "C:\Users\pc\Desktop\"
            SaveFileDialog1.Filter = "dat files (*.dat)|*.dat"
            SaveFileDialog1.FilterIndex = 2
            SaveFileDialog1.Title = "dat"
            SaveFileDialog1.RestoreDirectory = True
            If (SaveFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then
                fName = SaveFileDialog1.FileName
            End If

            For j = 0 To 0
                For x = 0 To DataGridView2.RowCount - 1

                    Dim tw As IO.TextWriter = New IO.StreamWriter(fName)
                    tw.Write(DataGridView2.Rows(j).Cells(0).Value & vbCr & vbLf & DataGridView2.Rows(j).Cells(1).Value & vbCr & vbLf & DataGridView2.Rows(j).Cells(2).Value & vbCr & vbLf & DataGridView2.Rows(j).Cells(3).Value & vbCr & vbLf & DataGridView2.Rows(j).Cells(4).Value & vbCr & vbLf & StrExport & "       " & DataGridView2.Rows(j).Cells(8).Value)



                    tw.Close()


                Next

            Next

            For Each row As DataGridViewRow In DataGridView2.Rows
                DataGridView2.Rows.Remove(row)
            Next

        Next





    Catch ex As Exception
        MsgBox("ERROR :" + ex.Message)
    End Try
    MsgBox("Exported", vbInformation, "Successful")

但我在下面遇到这个问题

enter image description here

1 个答案:

答案 0 :(得分:0)

修改 根据您的评论,我知道您不希望数据从数据库中丢失而是从网格中消失。我建议不要像你那样使用数据集。 DataSet有一组DataTable个对象,您可以操作它们。我还看到你要删除For Each循环中的每一行。您可以从表中删除这些行或在该表上调用clear方法,并且网格应该反映它。我已经离开原来的答案来解释为什么会这样。的修改

如果DataGridView不是带有更改事件通知的IBindingList,我只能告诉您,如果DataSource不是Future,则无法以编程方式从@RunWith(VertxUnitRunner.class) public class Chaining { private Vertx vertx = Vertx.vertx(); @Test public void futures_chaining(TestContext context) throws Exception { Async async = context.async(); firstOperation() .compose((outcome) -> { System.out.println(outcome); return secondOperation(); }) .compose(outcome -> { System.out.println(outcome); /* For stopping unit test we are returning this future for production use-case this would be Future.succeededFuture or Future.failedFuture depending on your method outcomes */ return Future.future(handle -> async.complete()); }); } private Future<String> firstOperation() { Future<String> future = Future.future(); vertx.setTimer(1000, delay -> future.complete("First Operation Complete")); return future; } private Future<String> secondOperation() { Future<String> future = Future.future(); vertx.setTimer(1000, delay -> future.complete("Second Operation Complete")); return future; } } 中删除行。 OdbcDataAdapter不在其继承链中的任何位置实现IBindingList。

如果您不愿意更改绑定的内容,可以重新运行查询以获取项目并再次绑定网格。