Interface我正在尝试将数据库中的数据显示到DataGridView,我成功地做到了这一点,但我希望以两种方式显示:1)显示所有记录和2)显示指定的记录。我可以使用两个组合框显示指定的记录:品牌和尺寸,通过从下拉列表中指定,数据显示在DataGridView上。但我现在想从下拉列表中显示所有品牌和所有尺寸,但它没有显示;它只显示两个查询中的一个。如何根据下拉列表的选择来显示代码?
我放置了一个if语句,检查用户是否选择了ComboBox1:所有轮胎和组合框2:所有轮胎,那么它应该执行所有查询。如果没有,用户只选择了一种类型的品牌和大小,那么它将显示只是与另一个查询。
接口:这是密码:
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
MysqlConn = New MySqlConnection
MysqlConn.ConnectionString = "server=localhost;userid=root;password=root;database=golden_star"
''connecting data grid with the database
Dim Sda As New MySqlDataAdapter
Dim dbdataset As New DataTable
Dim bSource As New BindingSource
Try
MysqlConn.Open()
Dim Query As String
Dim All As String
If ComboBox1.Text = "ALL TYRES" & ComboBox2.Text = "ALL SIZES" Then
All = "select * from golden_star.sales"
Command = New MySqlCommand(All, MysqlConn)
End If
Query = "select sale_id,date,brand,size,selling_unit_price,cost_unit_price,quantity,cost_of_goods,profit,total_cost_price from golden_star.sales where brand = '" + ComboBox1.Text + "' and size = '" + ComboBox2.Text + "' "
Command = New MySqlCommand(Query, MysqlConn)
Sda.SelectCommand = Command
Sda.Fill(dbdataset)
bSource.DataSource = dbdataset
DataGridView1.DataSource = bSource
Sda.Update(dbdataset)
MysqlConn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
MysqlConn.Dispose()
End Try
End Sub
1条答案
按热度按时间pcww981p1#
您的SQL命令没有选择相同的列:
你是如何定义网格的?柱是动态创建的还是固定的?
尝试更改
All
查询以选择与另一个查询相同的列。如果这不起作用,请显示新代码(使用else语句)和网格视图定义。同时检查组合框中是否定义了
ALL TYRES
。(应该是TYPES
吗?)我猜这是一个训练作业。如果这是一个实际上将在公共网站上使用的东西,你需要改变查询。在SQL命令中连接字符串是不安全的。