[英]how to move the datagridview record value column to another record column and then save it into the database in the vb.net
如何將datagridview記錄值列移動到另一個記錄列,然后將其保存到vb.net中的數據庫中。我的意思是將記錄值從“CIU”列移動到“CIUB”列並將記錄值移動從“DPR”列到“DPRB”列以及下面我附上我想要的帶有黃色標記的屏幕截圖。 什么是最好的推薦方式?我想用事件按鈕來做
Dim Path As String = Environment.CurrentDirectory
Dim cn As String = "provider=Microsoft.Jet.OLEDB.4.0; data source=" & Path & "; Extended Properties=dBase IV"
Dim source1 As New BindingSource()
Private Sub fillDataGridView1()
Try
Dim query As String = "SELECT * FROM GSDTS WHERE QTY > 0"
Using con As OleDbConnection = New OleDbConnection(cn)
Using cmd As OleDbCommand = New OleDbCommand(query, con)
'cmd.Parameters.AddWithValue("@PNM", ComboBox1.SelectedValue)
Using da As New OleDbDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
da.Dispose()
source1.DataSource = dt
'Me.DataGridView1.DataSource = dt
Me.DataGridView1.DataSource = source1
Me.DataGridView1.Refresh()
End Using
End Using
End Using
Catch ex As Exception
End Try
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
fillDataGridView1()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
connectionString = cn
con = New OleDbConnection(connectionString)
con.Open()
Dim sql As String = "UPDATE GSDTS SET CIUB = CIU, CIU = NULL WHERE QTY > 0"
cmd = New OleDbCommand(sql, con)
'cmd.Parameters.AddWithValue("@CODE", ComboBox1.SelectedValue)
cmd.ExecuteNonQuery()
con.Close()
Me.fillDataGridView1()
Catch myerror As OleDbException
MessageBox.Show("Error: " & myerror.Message)
Finally
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
For Each row As DataRow In source1.Rows
row("CIUB") = row("CIU")
row("CIU") = DBNull.Value
Next
End Sub
您所描述的與網格完全無關。 那是給用戶的。 您可以在BindingSource
或DataTable
中執行此操作。 這是一個簡單的循環:
For Each row As DataRow In myDataTable.Rows
row("CIUB") = row("CIU")
row("CIU") = DBNull.Value
'...
Next
事實上,您甚至不需要檢索任何數據,因為您可以在數據庫上的 SQL 中完成所有操作:
UPDATE GSDTS
SET CIUB = CIU, CIU = NULL
WHERE QTY > 0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.