[英]vb.net how do i add a new rows (and update) from datagridview programmatically to sql database
問一個關於我以前的問題的新問題。
就是這樣,到目前為止,我能夠使以下代碼正常工作(感謝@JADE幫助我,(很多):)
Dim connstr As String = "server=midtelephone\sqlexpress; database=testdb; user= sa; password=sa;"
cmdconn = New SqlConnection
cmd = New SqlCommand
cmdconn.ConnectionString = connstr
cmd.Connection = cmdconn
cmdconn.Open()
Dim period, VOUCH_AMT, INDIVIDUAL_AMT, check_no, D_MAILED, DIR_NO As String
For i As Integer = 0 To Me.DataGridView1.Rows.Count - 1
With Me.DataGridView1.Rows(i)
If IsDBNull(.Cells(0).Value()) OrElse .Cells(0).Value() Is Nothing OrElse .Cells(0).Value().ToString().Trim() = "" Then
period = ""
Else
period = .Cells(0).Value()
End If
If IsDBNull(.Cells(1).Value()) OrElse .Cells(1).Value() Is Nothing OrElse .Cells(1).Value().ToString().Trim() = "" Then
VOUCH_AMT = "0"
Else
VOUCH_AMT = .Cells(1).Value()
End If
If IsDBNull(.Cells(2).Value()) OrElse .Cells(2).Value() Is Nothing OrElse .Cells(2).Value().ToString().Trim() = "" Then
INDIVIDUAL_AMT = "0"
Else
INDIVIDUAL_AMT = .Cells(2).Value()
End If
If IsDBNull(.Cells(3).Value()) OrElse .Cells(3).Value() Is Nothing OrElse .Cells(3).Value().ToString().Trim() = "" Then
check_no = ""
Else
check_no = .Cells(3).Value()
End If
If IsDBNull(.Cells(4).Value()) OrElse .Cells(4).Value() Is Nothing OrElse .Cells(4).Value().ToString().Trim() = "" Then
D_MAILED = ""
Else
D_MAILED = .Cells(4).Value()
End If
If IsDBNull(.Cells(5).Value()) OrElse .Cells(5).Value() Is Nothing OrElse .Cells(5).Value().ToString().Trim() = "" Then
DIR_NO = ""
Else
DIR_NO = .Cells(5).Value()
End If
End With
cmd.CommandText = "insert into tobee.EBD_BILLHISTORY(period, vouch_amt, individual_amt, check_no, d_mailed, dir_no)values" & _
"('" & period.Replace("'", "''") & "'," & VOUCH_AMT & "," & INDIVIDUAL_AMT & ",'" & check_no.Replace("'", "''") & "','" & D_MAILED.Replace("'", "''") & "', '" & DIR_NO.Replace("'", "''") & "')"
cmd.ExecuteNonQuery()
MsgBox("Saved")
Next
cmdconn.Close()
End Sub
現在的問題是,如果我想以編程方式添加新行(我已經將dgv設置為以編程方式進行編輯)。 度量標准或下一步程序是什么? 我有一個編輯按鈕,可以編輯datagridview,如下所示:
Private Sub btnEditmain_Click(sender As Object, e As EventArgs) Handles btnEditmain.Click
DataGridView1.AllowUserToAddRows = True
DataGridView1.BeginEdit(True)
btnSave.Enabled = True
End Sub
(同樣,當我檢查數據庫中的表時,似乎單擊“保存”按鈕后沒有進行任何更改)。 幫我。 提前致謝。 :)
您是否嘗試將DataGridView更改為ListView並啟用對ListView的編輯? 如果分別指定了命令,則插入,編輯和刪除按鈕將出現在ListView上。
我也想這樣做,但是經過長時間的研究,這是最好的。 加載表代碼,然后在編輯DataGridView后添加一個按鈕。
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.TableAdapter.Fill(Me.DataSet.YourTable)
Me.TableAdapter.Fill(Me.DataSet.YourTable)
DataGridView1.[ReadOnly] = False
adap.Fill(dt)
BindingSource.DataSource = dt
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
adap.Update(dt)
' adap.UpdateCommand(dt)
' MessageBox.Show("Saved successfully")
Catch ex As Exception
' MessageBox.Show("Error updating database")
End Try
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.