Using a combobox to query the database

后端 未结 2 1864
轮回少年
轮回少年 2021-01-29 08:23

I have all the days of the week in a combobox, yet when selecting a day all the days are shown in my data grid... How can I use the one selected value from the combobox to searc

相关标签:
2条回答
  • 2021-01-29 08:49

    You can Simply make a filter in your datagridview using following code

    Dim column As String = 'Your weekday column Name in Datagridview
    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    
        Dim rows = ds.Tables(0).Select("Convert(" & column & ", 'System.String') LIKE '*" + ComboBox1.SelectedItem + "*'")
    
        Try
            DGTimeTable.DataSource = rows.CopyToDataTable
        Catch ex As Exception
        End Try
    End Sub
    
    0 讨论(0)
  • 2021-01-29 09:15

    you are passing cbDay as a string, try changing this line

    Dim SqlQuery As String = "SELECT Time, Activity, Equipment FROM TimeTable WHERE Day = cbDay"
    

    to something like this

    Dim SqlQuery As String = String.Format("SELECT Time, Activity, Equipment FROM TimeTable WHERE Day = {0}", cbDay.Text)
    
    0 讨论(0)
提交回复
热议问题