2017-06-18 66 views
-1

我想使用后删除数据的GridView的行从数据库 我用这个代码如何删除vb.net中的数据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") 

查询SQL,但我得到以下

enter image description here

+1

如果你无法翻译错误或事件类型,所以我们可以将其粘贴到翻译器中,为什么有人需要帮忙?请阅读[问]并采取[旅游] – Plutonix

回答

0

编辑 立足这个问题在你的评论中,我明白你不希望数据库中的数据从网格中消失。我建议不要像你所做的那样使用数据集。 A DataSet具有可以操纵的DataTable对象的集合。我也看到你正在删除For Each循环中的每一行。您可以从表中删除这些行或调用该表上的clear方法,并且您的网格应反映它。我已经留下原始的答案来解释为什么会发生这种情况。 编辑

而不能告诉错误是,我只是告诉你,你不能以编程方式从DataGridView删除一行,如果DataSource是不变化的事件通知的IBindingList的是什么。 OdbcDataAdapter不会在其继承链的任何地方实现IBindingList。

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

+0

我不想从数据库中删除日期,但我想只是删除datagridview的行为另一个查询 –