VB.NET 将值从文本框传递到数据网格视图

VB.NET Pass values from textbox to datagridview

我想使用以下代码将文本框的值传递给 datagridview。

  Private Sub txtDrCr_Leave(sender As Object, e As System.EventArgs) Handles txtDrCr.Leave

    Dim nR As Integer
    nR = TxtList.Rows.Add()
    TxtList.Item("stsno", nR).Value = nR + 1
    TxtList.Item("stAcctCode", nR).Value = txtAccountCode.Text
    TxtList.Item("stAcctName", nR).Value = txtAccountName.Text
    TxtList.Item("stBranch", nR).Value = txtBranchName.Text
    TxtList.Item("stDescription", nR).Value = txtDescription.Text
    TxtList.Item("stAmount", nR).Value = txtAmount.Text
    TxtList.Item("stDrCR", nR).Value = txtDrCr.Text
    cleartextboxes()
End Sub

每当我离开 txtDRCR 时,此代码都会在 gridview 中添加两行。我也附上了快照。请需要帮助。谢谢

请使用以下代码。

    Dim nR As Integer =0;
    Dim Row As Integer =0;
    TxtList.Rows.Add()
    Row = TxtList.Rows.Count - 2;

    TxtList("stsno", Row ).Value = nR + 1
    TxtList("stAcctCode", Row ).Value = txtAccountCode.Text
    TxtList("stAcctName", Row ).Value = txtAccountName.Text
    TxtList("stBranch", Row ).Value = txtBranchName.Text
    TxtList("stDescription", Row ).Value = txtDescription.Text
    TxtList("stAmount", Row ).Value = txtAmount.Text
    TxtList("stDrCR", Row ).Value = txtDrCr.Text
    TxtList.Refresh()

我相信这就是您想要的一般概念。

Private Sub btnAddList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddList.Click
       Dim i As Integer
       With dgvIssuedBooks
           ' Write to cell (0,0)
           .Rows(i).Cells("CallNumber").Value = txtCallNo2.Text
           .Rows(i).Cells("Title").Value = txtTitle2.Text
           .Rows(i).Cells("AccessionNumber").Value = txtAccess2.Text
           .Rows(i).Cells("Borrower").Value = dgvSearch.Rows(i).Cells("FullName").Value
           .Rows(i).Cells("ID").Value = txtShow.Text

       End With
       txtAccess2.Text = ""
       txtCallNo2.Text = ""
       txtTitle2.Text = ""
   End Sub

仔细检查所有命名对象的拼写。另外,如你所知,一遍又一遍地按 F11 会让你逐行浏览代码,这样你就可以看到它运行时实际发生了什么。