如何通过位于另一个表单上的文本框更新datagridview中的选定行?

时间:2014-02-27 01:16:22

标签: sql-server vb.net winforms datagridview

大家好,我到目前为止一直在使用我的系统并且它已经差不多完成了,但有一件事我还不能解决(除非你帮我解决)。 所以这是我的代码:

Private Sub btnEditmain_Click(sender As Object, e As EventArgs) Handles btnEditmain.Click

    Try
        editdgv()
        Form2.Show()
        DataGridView2.AllowUserToAddRows = True
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

这是private sub editdgv

Private Sub editdgv()
    Dim i = DataGridView2.CurrentRow.Index

    With DataGridView2
        Form2.txtPeriod.Text = IIf(IsDBNull(.Rows(i).Cells("period").Value), " ", .Rows(i).Cells("period").Value)
        Form2.txtVouch.Text = IIf(IsDBNull(.Rows(i).Cells("vouch_amt").Value), " ", .Rows(i).Cells("vouch_amt").Value)
        Form2.txtIndivAmt.Text = IIf(IsDBNull(.Rows(i).Cells("individual_amt").Value), " ", .Rows(i).Cells("individual_amt").Value)
        Form2.txtCheckno.Text = IIf(IsDBNull(.Rows(i).Cells("check_no").Value), " ", .Rows(i).Cells("check_no").Value)
        Form2.txtDmailed.Text = IIf(IsDBNull(.Rows(i).Cells("D_MAILED").Value), " ", .Rows(i).Cells("D_MAILED").Value)
        Form2.txtDirno.Text = IIf(IsDBNull(.Rows(i).Cells("DIR_NO").Value), " ", .Rows(i).Cells("DIR_NO").Value)
        Form2.txtYrlvl.Text = IIf(IsDBNull(.Rows(i).Cells("year_student").Value), " ", .Rows(i).Cells("year_student").Value)
        Form2.txtUpdatedBy.Text = IIf(IsDBNull(.Rows(i).Cells("who_updated").Value), " ", .Rows(i).Cells("who_updated").Value)
    End With
End Sub

我能够将datagridview中所选行的值传递给另一个表单(文本框) 从那里开始,一切都很好..

现在我要做的是更新现在在我的文本框中的所选数据网格视图。 点击按钮。

现在我坚持保存我在文本框中所做的更改,因此它将在数据库中更新。

请帮我看看如何通过按钮点击更新文本框中datagridview中的选定行。 谢谢!

Public Class Form2

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click

End Sub

End Class

1 个答案:

答案 0 :(得分:0)

我在记事本中创建了这段代码。所以,它可能有一些问题。

FORM 1
Private Sub btnEditmain_Click(sender As Object, e As EventArgs) Handles btnEditmain.Click

   Try
        Dim dt As DataTable = TryCast(DataGridView2.DataSource, DataTable)
        Dim dr As DataRow = dt.Rows(DataGridView2.CurrentRow.Index)
        Dim frm As New Form2
        frm.dr = dr
        frm.Show()
        DataGridView2.AllowUserToAddRows = True
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub


FORM 2

Public dr As DataRow = Nothing

Private Sub Form2_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    With dr
        txtPeriod.Text = IIf(IsDBNull(.Item("period")), vbNullString, .Item("period").ToString)
        txtVouch.Text = IIf(IsDBNull(.Item("vouch_amt")), vbNullString, .Item("vouch_amt").ToString)
        txtIndivAmt.Text = IIf(IsDBNull(.Item("individual_amt")), vbNullString, .Item("individual_amt").ToString)
        txtCheckno.Text = IIf(IsDBNull(.Item("check_no")), vbNullString, .Item("check_no").ToString)
        txtDmailed.Text = IIf(IsDBNull(.Item("D_MAILED")), vbNullString, .Item("D_MAILED").ToString)
        txtDirno.Text = IIf(IsDBNull(.Item("DIR_NO")), vbNullString, .Item("DIR_NO").ToString)
        txtYrlvl.Text = IIf(IsDBNull(.Item("year_student")), vbNullString, .Item("year_student").ToString)
        txtUpdatedBy.Text = IIf(IsDBNull(.Item("who_updated")), vbNullString, .Item("who_updated").ToString)
    End With
End Sub

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
    With dr
        .Item("period") = txtPeriod.Text
        .Item("vouch_amt") = txtVouch.Text
        .Item("individual_amt") = txtIndivAmt.Text
        .Item("check_no") = txtCheckno.Text
        .Item("D_MAILED") = txtDmailed.Text
        .Item("DIR_NO") = txtDirno.Text
        .Item("year_student") = txtYrlvl.Text
        .Item("who_updated") = txtUpdatedBy.Text
    End With    
    dr.Table.AcceptChanges
End Sub