我想使用后删除数据的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,但我得到以下
如果你无法翻译错误或事件类型,所以我们可以将其粘贴到翻译器中,为什么有人需要帮忙?请阅读[问]并采取[旅游] – Plutonix