简体   繁体   English

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

[英]vb.net how do i add a new rows (and update) from datagridview programmatically to sql database

asking a new question with regards to my previous problem. 问一个关于我以前的问题的新问题。

so this is how it goes, so far i was able make the following code to work (thanks to @JADE for helping me out, (a lot ) :) 就是这样,到目前为止,我能够使以下代码正常工作(感谢@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

now the problem is if i want to add new rows programmatically (i already set the dgv to edit programmatically ). 现在的问题是,如果我想以编程方式添加新行(我已经将dgv设置为以编程方式进行编辑)。 what are the metrics or the next procedures to do? 度量标准或下一步程序是什么? i have an edit button which enables the datagridview to be edited, something like this: 我有一个编辑按钮,可以编辑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

( also, as i check the table in my database, it seems that there's no changes made after clicking the save button ) pls. (同样,当我检查数据库中的表时,似乎单击“保存”按钮后没有进行任何更改)。 help me. 帮我。 thanks in advance. 提前致谢。 :) :)

Did you try to change the DataGridView to ListView and enable editing on ListView ? 您是否尝试将DataGridView更改为ListView并启用对ListView的编辑? The insert,edit and delete buttons will appear on the ListView if you have specified their commands respectively. 如果分别指定了命令,则插入,编辑和删除按钮将出现在ListView上。

I wanted to do the same but after long research this was the best. 我也想这样做,但是经过长时间的研究,这是最好的。 Load the table code then add a button once they edit the DataGridView. 加载表代码,然后在编辑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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM