【问题标题】:VB.NET Pass values from textbox to datagridviewVB.NET 将值从文本框传递到 datagridview
【发布时间】:2018-05-01 12:03:32
【问题描述】:

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

【问题讨论】:

  • 你需要调试你的代码。如果你不知道怎么做,做一些研究和学习。在此处发布问题之前,所有开发人员都需要知道如何以及您需要这样做。正确调试会向您显示这两行添加的位置,即使您不知道如何修复它,至少您可以向我们提供相关信息。
  • 你应该讨论解决方案,而不是写与我的问题无关的东西。
  • 所有信息都写在问题中。告诉我您还需要什么信息。
  • 我的评论与您的​​问题有关。在此处发布之前,您需要尽您所能为自己解决任何问题。如果你还没有调试,那么你还没有这样做,所以你不应该在这里发帖。如果你刚刚开始,那么你可能不知道,这就是我告诉你的原因。现在你知道了,你可以做你应该做的。就像我说的,每个开发人员都需要能够调试,所以没有比现在更好的时间来学习如何调试了。我们不在这里,因此您不必尝试解决问题。我们随时为您解决已尝试解决但无法解决的问题。
  • 我告诉过你要调试你的代码。你这样做了吗?如果不是,为什么不呢?如果是这样,我告诉过你它会显示这两行是在哪里创建的,那么你为什么不告诉我们呢?调试你的代码。如果您还没有这样做,那么您还没有尝试解决问题,如果您还没有尝试解决问题,那么在这里发布还为时过早。当您尝试但无法解决问题时在此处发布。

标签: vb.net datagridview textbox


【解决方案1】:

请使用以下代码。

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-31
      • 1970-01-01
      • 2016-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      相关资源
      最近更新 更多