【问题标题】:Send text from textboxes to datagrid将文本从文本框发送到数据网格
【发布时间】:2014-04-12 14:48:01
【问题描述】:

我的表单上有文本框,我希望将那里的文本发送到数据网格。我写了以下内容:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim itemName As String = txtItem.Text
    Dim qty As Double = CDbl(txtQTY.Text)
    Dim price As Double = CDbl(txtPrice.Text)
    Dim Total As Double = price * qty

    txtTotal.Text = Convert.ToString(Total)

    Dim row As Integer = grdNewInvoice.Rows.Count

    Dim data As TextBox() = New TextBox() {txtItem, txtQTY, txtPrice, txtTotal}
    grdNewInvoice.Rows.Add()

    For i As Integer = 0 To data.Length - 1
        grdNewInvoice(i, row).Value = data(0)

    Next


End Sub

但我在数据网格行中得到以下信息:System.Windows.Forms.TextBox, Text: [文本框字符串]

我也尝试了以下代码以确保我的数据网格上的设置没有问题:

'grdNewInvoice.Rows(0).Cells(0).Value = itemName
    'grdNewInvoice.Rows(0).Cells(1).Value = qty
    'grdNewInvoice.Rows(0).Cells(2).Value = price
    'grdNewInvoice.Rows(0).Cells(3).Value = Total

效果很好,文本按预期输入,但由于我将写入数据网格上的多行,我需要使用循环。

我在这里做错了什么?

【问题讨论】:

    标签: vb.net datagrid


    【解决方案1】:

    一种方法是使用字符串数组列表。

    Dim row As Integer = grdNewInvoice.Rows.Count
    
    Dim data As New List(Of String())
    data.Add({txtItem.Text, txtQTY.Text, txtPrice.Text, txtTotal.Text})
    data.Add({"Item2", "qtr2", "price2", "total2"})
    data.Add({"Item3", "qty3", "price3", "total3"})
    
    For i As Integer = 0 To data.Count - 1
        grdNewInvoice.Rows.Add(data(i))
    Next
    

    【讨论】:

    • 谢谢,您之前删除的答案也很好用。
    • 我删除了它,因为您说要使用循环进行添加。
    猜你喜欢
    • 2012-08-29
    • 1970-01-01
    • 2012-07-29
    • 2013-12-12
    • 2012-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多