VB.NET将值从文本框传递到datagridview

时间:2018-04-21 07:34:28

标签: vb.net datagridview textbox

我想使用下面的代码将文本框的值传递给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中添加两行。我也附上了快照。请需要帮助。感谢。

ImageAttached

2 个答案:

答案 0 :(得分:0)

请使用以下代码。

    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()

答案 1 :(得分:0)

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

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会让您逐行执行代码,这样您就可以看到它运行时的实际情况。