简体   繁体   中英

Vb.Net Filter Datagridview using combo box

i am having trouble im using visual studio in my other windows form same code it is working properly but i am using it now on another form and it doesn't filter anymore i don't know why.

I am trying to display a list in datagrid view and filter it using two combobox. Here is a example database of the subject:

-------------------------------------------------------------------------------
|subject_id|subject_name|subject_code|subject_units|    sem        |year_level
------------------------------------------------------------------------------
|    1     |  MATH      | Math101    |     3       |First Semester | First Year
|    2     |  English   | ENG101     |     3       |First Semester | First Year
|    3     |  Prgrming  | IT101      |     3       |Second Semester| First Year
|    4     |  Networking| IT203      |     3       |Second Semester| First Year

Here is my code.

myconn = New MySqlConnection
    myconn.ConnectionString = connstring
    myconn.Open()
    tables = ds.Tables
 ds = New DataSet
    tables = ds.Tables
    Dim Query As String
    Query = "selct * from subject_bsit"

'*** CHECKING IF ALL REQUIRED FIELDS ARE PRESENT *****
    If ComboBox1.Text = "" Or ComboBox2.Text = "" Then
        MessageBox.Show("Complete all fields.")
        Exit Sub
    End If



 If (ComboBox1.Text = "All") And (ComboBox2.Text = "First Semester") Then
            da = New MySqlDataAdapter("Select subject_id as 'ID',subject_name as 'SUBJECT',subject_code as 'SUBJECT CODE',subject_units as 'UNITS',sem as 'Semester',year_level as 'YEAR LEVEL' from subject_bsit  where sem = '" + ComboBox2.Text + "' or year_level = '" + ComboBox1.Text + "' ", myconn)
            da.Fill(ds, "subject_bsit")

            Dim view As New DataView(tables(0))
            source1.DataSource = view
            DataGridView1.DataSource = view
            DataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
        End If

在此处输入图片说明

As you can see in the sample image the results must be All year Level First year to Fourth year but only ALL First Semester subject but in the picture it also show the Second semester.

It makes me confuse because i have the exact same code in other form but it is working properly. Please help thank you so much.

重新启动Visual Studio解决了该问题。

You just need to replace the logical operator “or” with “and” in your sql string like this way:

from subject_bsit where sem = '" + ComboBox2.Text + "' and year_level = '" + ComboBox1.Text + "' ", myconn)

I hope this can help you bro, ^_^

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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