简体   繁体   English

使用组合框特定选项显示数据库中的数据:ALL SIZES

[英]Display data from database using combo box specific option: ALL SIZES

Interface I'm trying to display data from the database to DataGridView which I successfully did but I want to display in two ways: 1) to display all records and 2) to display specified records.界面我正在尝试将数据从数据库显示到 DataGridView,我成功地做到了,但我想以两种方式显示:1) 显示所有记录和 2) 显示指定记录。 I was able to display specified records using two Combo Boxes: brand and sizes by specifying from their dropdown list and data are shown on the DataGridView.我能够使用两个组合框显示指定的记录:通过从下拉列表中指定品牌和尺寸,数据显示在 DataGridView 上。 But I now want to display ALL BRANDS and ALL SIZES from the drop-down list but it's not displaying;但是我现在想从下拉列表中显示所有品牌和所有尺寸,但它没有显示; it only displays either of the queries.它只显示其中一个查询。 How can put the code display based on the selection of the dropdown list?如何根据下拉列表的选择来放置代码显示?

I have put an if statement that checks if the user has selected ComboBox1: ALL TYRES and ComboBox2: ALL TYRES then it should execute ALL QUERY.我已经放置了一个 if 语句来检查用户是否选择了 ComboBox1: ALL TIRES 和 ComboBox2: ALL TIRES 然后它应该执行 ALL QUERY。 If not and the user has selected just one type of brand and size then it would display just that with another query.如果不是,并且用户只选择了一种类型的品牌和尺码,那么它将只显示另一个查询。

The interface: This is the code:界面: 这是代码:

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

Your SQL commands are not selecting the same columns.您的 SQL 命令未选择相同的列。

All = "select * from golden_star.sales" All = "从 golden_star.sales 中选择 *"

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 + "' " 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 其中品牌 = '" + ComboBox1.Text + "' and size = '" + ComboBox2.Text + " ’”

How have you defined the grids?你是如何定义网格的? Are the columns being created dynamically or are they fixed?列是动态创建的还是固定的?

Try changing the All query to select the same columns as the other query.尝试将 All 查询更改为 select 与其他查询相同的列。

All = "select sale_id,date,brand,size, selling_unit_price, cost_unit_price, quantity, cost_of_goods,profit,total_cost_price from golden_star.sales"

If this doesn't work, please show the new code (with the else statements) and the gridview definition.如果这不起作用,请显示新代码(带有 else 语句)和 gridview 定义。 Also check to see if "ALL TYRES" is defined in your combobox. (Should it be TYPES?)还要检查您的 combobox 中是否定义了“ALL TYRES”。(应该是 TYPES?)

I am guessing this is a training assignment.我猜这是一项培训任务。 If this is something that will actually be used on a public website, you need to change the query.如果这是实际将在公共网站上使用的内容,则需要更改查询。 It is not safe to concatenate strings in the SQL command.在 SQL 命令中连接字符串是不安全的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM