vb.net如何以编程方式从datagridview向sql数据库添加新行(和更新)

时间:2014-02-10 07:01:45

标签: sql sql-server vb.net datagridview

询问有关我之前问题的新问题。

这是怎么回事, 到目前为止,我能够使下面的代码工作(感谢@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

(另外,当我检查我的数据库中的表时,似乎单击保存按钮后没有进行任何更改)请参阅。帮我。提前致谢。 :)

2 个答案:

答案 0 :(得分:0)

您是否尝试将DataGridView更改为ListView并在ListView上启用编辑? 如果您已分别指定了命令,则插入,编辑和删除按钮将出现在ListView上。

答案 1 :(得分:0)

我想做同样的事情,但经过长时间的研究,这是最好的。加载表代码,然后在编辑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