如何使用sql中的vb.net在datagrid中插入多个数据

时间:2017-11-09 02:25:23

标签: sql vb.net datagridview datagrid

这是我的代码,用于在datagridview中添加数据 -

Dim rdr1 As MySqlDataReader
If Trim(txteventname.Text) = "" Or Trim(cbomenulist.Text) = "" Or Trim(cbopax.Text) = "" Or txteventname.Text = "" Or cbomenulist.Text = "" Or cbopax.Text = "" Then
    MsgBox("Invalid Input of Data", vbInformation, "Validation")
Else

Dim strSql As String


Call myconnection()
Con.Open()
strSql = "SELECT p.product_no,p.product_name, p.product_pcs, p.product_price,d.menu_no FROM tblmenu as p,tblmenu_details as d where p.menu_no=d.menu_no and d.menu_name like '" & cbomenulist.Text & "%'"
Dim cmd11 As New MySqlCommand(strSql, Con)
rdr1 = cmd11.ExecuteReader

If rdr1.Read Then
    Dim productno As New DataGridViewColumn
    Dim productname As New DataGridViewColumn
    Dim quantity As New DataGridViewColumn
    Dim productprice As New DataGridViewColumn
    Dim productmenu As New DataGridViewColumn

    productno = rdr1("product_no")
    productname = rdr1("product_name")
    quantity = rdr1("product_pcs")
    productprice = rdr1("product_price")
    productmenu = rdr1("menu_no")


    dtgproducts.Columns.Add(productno).ToString()
    dtgproducts.Columns.Add(productname).ToString()
    dtgproducts.Columns.Add(quantity).ToString()
    dtgproducts.Columns.Add(productprice).ToString()
    dtgproducts.Columns.Add(productmenu).ToString()

    For i As Integer = 0 To dtgproducts.Rows.Count - 1 Step +1
        Dim rowsa As Boolean = False
        If rdr1.Read Then
            Dim row As DataGridViewRow = dtgproducts.Rows(i)
            If dtgproducts.Rows.Count = 0 Then
                For j As Integer = 0 To dtgproducts.Rows.Count Step +1
                    If row.Cells(0).Value.ToString() = dtgproducts.Rows(j).Cells(0).Value.ToString Then
                        rowsa = True
                    End If
                Next
                If rowsa = False Then
                    dtgproducts.Rows.Add(row.Cells(0).Value.ToString)
                    dtgproducts.Rows(dtgproducts.Rows.Count - 1).Cells(0).Value = rdr1("product_no")
                    dtgproducts.Rows(dtgproducts.Rows.Count - 1).Cells(1).Value = rdr1("product_name")
                    dtgproducts.Rows(dtgproducts.Rows.Count - 1).Cells(2).Value = rdr1("product_pcs")
                    dtgproducts.Rows(dtgproducts.Rows.Count - 1).Cells(3).Value = rdr1("product_price")
                    dtgproducts.Rows(dtgproducts.Rows.Count - 1).Cells(4).Value = rdr1("menu_no")
                End If
            End If
            'dtb = dtgproducts.Rows(i).Cells(3)
        End If
    Next
End If


End If

Con.Close()

这里,数据库中列出的所有数据都是插入数据的销售点样式,但是我在datagridview中插入了多个数据。

我想在datagrid中插入多个数据。我怎么能用这段代码呢?

0 个答案:

没有答案