将数据从TextBox传递到VB.NET中的DataGridView [英] Pass data from TextBox to DataGridView in VB.NET

查看:386
本文介绍了将数据从TextBox传递到VB.NET中的DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表单中有两个DataGridView。一个命名为grd_product,另一个是grd_order。当我单击grd_product中的单元格时,它将在TextBox中显示该产品的详细信息。所以在用户插入数量之后,当用户点击btn_add时,据说会在grd_order中添加一个新行。但是我收到一个错误。

 公共类frm_customer_orderproduct 
Dim current_id As String
Private Sub frm_customer_orderproduct_Load发件人As System.Object,e As System.EventArgs)处理MyBase.Load
'Dim regDate As Date = Date.Today()

lbl_user.Text =& custName& !

refresh_grid()
get_current_id()
End Sub
Private Sub refresh_grid()
Dim mysql As String =SELECT FLD_PRODUCT_ID,FLD_PRODUCT_NAME,FLD_PRODUCT_PRICE FROM TBL_PRODUCTS
Dim mydatatable As New DataTable
Dim myreader As New OleDb.OleDbDataAdapter(mysql,myconnection)
myreader.Fill(mydatatable)
grd_product.DataSource = mydatatable
End Sub
Private Sub clear_fields()
txt_id.Text =
txt_name.Text =
txt_price.Text =
txt_qty.Text =
End Sub
Private Sub get_current_id()
Dim current_row As Integer = grd_product.CurrentRow.Index
current_id = grd_product(0,current_row).Value

txt_id.Text = current_id
txt_name.Text = grd_product(1,current_row).Value
txt_price.Text = grd_product(2,current_row).Value
End Sub

Private Sub grd_product_CellClick(sender As Object,e As System.Windows.Forms.DataGridViewCellEventArgs)Handles grd_product.CellClick
get_current_id()
End Sub

Private Sub btn_add_Click(发件人As System.Object,e As System.EventArgs)处理btn_add.Click
如果txt_qty.Text =然后
MsgBox(请插入数量)
Else
Dim dt As New DataTable
Dim dr As DataRow

dt.Columns.Add(Product ID)
dt.Columns.Add(Product Name)
dt.Columns.Add(Price)
dt.Columns.Add(Quantity)
dr = dt.NewRow
dr(id)= txt_id.Text
dr(name)= txt_name.Text
dr(price)= txt_price.Text
dr(qty)= txt_qty.Text
dt.Rows.Add (dr)
grd_order.DataSource = dt

如果

End Su b
结束类




假设用户点击添加无限制的产品后,它将被添加到grd_order

解决方案

很明显,datatable c> dt 的列名称与datarows不一样

你应该使用

  dr(Product ID)= 
dr(Product Name)=
dr(Price)=
dr(Quantity)=

  dr(0)= 
dr(1)=
dr(2)=
dr(3)=






在我看来,您可以尝试以下方式添加文本框va对datagirdview而不是使用datatable

 如果修剪(txt_qty.Text)=然后
MsgBox(请插入数量)
Else
Dim rnum As Integer = datagridviewname.Rows.Add()
datagridviewname.Rows.Item(rnum).Cells(name_of_productid_column)。Value = txt_id.Text
datagridviewname.Rows.Item(rnum).Cells(name_of_productname_column)。Value = txt_name.Text
datagridviewname.Rows.Item(rnum).Cells(name_of_price_column)。Value = txt_price.Text
datagridviewname.Rows.Item(rnum).Cells(name_of_qty_column)。Value = txt_price.Text
如果


I have two DataGridView in my form. One named grd_product and another is grd_order. When i click a cell in grd_product, it will display the detail of the product in the TextBox. So after the user insert the quantity, supposedly a new row will be added in grd_order when the user click btn_add. But i get an error instead.

Public Class frm_customer_orderproduct
    Dim current_id As String
    Private Sub frm_customer_orderproduct_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'Dim regDate As Date = Date.Today()

        lbl_user.Text = "Hi, " & custName & "!"

        refresh_grid()
        get_current_id()
    End Sub
    Private Sub refresh_grid()
        Dim mysql As String = "SELECT FLD_PRODUCT_ID, FLD_PRODUCT_NAME, FLD_PRODUCT_PRICE FROM TBL_PRODUCTS"
        Dim mydatatable As New DataTable
        Dim myreader As New OleDb.OleDbDataAdapter(mysql, myconnection)
        myreader.Fill(mydatatable)
        grd_product.DataSource = mydatatable
    End Sub
    Private Sub clear_fields()
        txt_id.Text = ""
        txt_name.Text = ""
        txt_price.Text = ""
        txt_qty.Text = ""
    End Sub
    Private Sub get_current_id()
        Dim current_row As Integer = grd_product.CurrentRow.Index
        current_id = grd_product(0, current_row).Value

        txt_id.Text = current_id
        txt_name.Text = grd_product(1, current_row).Value
        txt_price.Text = grd_product(2, current_row).Value
    End Sub

    Private Sub grd_product_CellClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grd_product.CellClick
        get_current_id()
    End Sub

    Private Sub btn_add_Click(sender As System.Object, e As System.EventArgs) Handles btn_add.Click
        If txt_qty.Text = "" Then
            MsgBox("Please insert quantity")
        Else
            Dim dt As New DataTable
            Dim dr As DataRow

            dt.Columns.Add("Product ID")
            dt.Columns.Add("Product Name")
            dt.Columns.Add("Price")
            dt.Columns.Add("Quantity")
            dr = dt.NewRow
            dr("id") = txt_id.Text
            dr("name") = txt_name.Text
            dr("price") = txt_price.Text
            dr("qty") = txt_qty.Text
            dt.Rows.Add(dr)
            grd_order.DataSource = dt

        End If

    End Sub
End Class

Supposedly, after the user click add for unlimited products, it will be added into grd_order

解决方案

It's pretty clear that columns name of datatable dt is not same as datarows

you should use

dr("Product ID")=
dr("Product Name")=
dr("Price")=
dr("Quantity")=

or

    dr(0)=
    dr(1)=
    dr(2)=
    dr(3)=


In my opinion you can try following way to add textbox values to datagirdview instead of using datatable

If Trim(txt_qty.Text) = "" Then
            MsgBox("Please insert quantity")
        Else
            Dim rnum As Integer = datagridviewname.Rows.Add()
            datagridviewname.Rows.Item(rnum).Cells("name_of_productid_column").Value = txt_id.Text
            datagridviewname.Rows.Item(rnum).Cells("name_of_productname_column").Value = txt_name.Text
            datagridviewname.Rows.Item(rnum).Cells("name_of_price_column").Value = txt_price.Text
            datagridviewname.Rows.Item(rnum).Cells("name_of_qty_column").Value = txt_price.Text
End If

这篇关于将数据从TextBox传递到VB.NET中的DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆