单击vb中的按钮时,在datagridview中更改数据表。净

时间:2016-09-23 03:21:47

标签: sql-server vb.net datagridview

我有2个表,我想要的是当我单击第一个按钮时,第一个表将加载到Datagridview中,当我单击另一个第二个按钮时,它将加载另一个表。但问题是,我只会使用1个Datagridview。这可能吗?我不想使用其他Datagridview视图或其他表单。我想在1个表单中使用1个datagridview。下面是我加载到表的数据连接。我正在使用SQL Server。

 Dim cn As New SqlConnection("Data Source=IT-ASSISTANT\sqlexpress;Initial   Catalog=KIS;Integrated Security=True")
 Dim cmd As New SqlCommand
 Dim da As New SqlDataAdapter
 Dim dt As New DataTable

enter image description here

表1:

cn.Open()
With cmd
    .Connection = cn
    .CommandText = "Select IDnumber as [ID Number], Productname as [Product 
                    Name], Brand, UnitPrice as [Unit Price], Stocks, UnitType as 
                   [Unit Type], ItemLocation as [Item Location], Remarks from 
                   MainForm"
End With
dt.Clear()
With da
    .SelectCommand = cmd
    .Fill(dt)
End With
DGVdata.DataSource = dt
cn.Close()
End Sub

表2:

cn.Open()
With cmd
    .Connection = cn
    .CommandText = "Select IDnumber as [ID Number], itmDescription as [Item  
                    Description], Supplier, UnitPrice as [Unit Price], Stocks, 
                    nsnumber as [Next Number], itemLoc as [Item Location], 
                    Remarks from FormsTable"
End With
dt.Clear()
With da
    .SelectCommand = cmd
    .Fill(dt)
End With
DGVdata.DataSource = dt
cn.Close()
End Sub

0 个答案:

没有答案