Loading a Combo Box from a SQL Database and change Data Grid View to Data selected from a Combo Box

Gary Simpson 471 Reputation points
2023-04-25T14:02:15.9233333+00:00

Hi Good People, I have a Data Grid View and when the form Loads, The Data Grid View is Populated with all my Data from my SQL Database. I also Have two Combo Boxes (CmbxDriverName) and (CmbxWeekNumber) I also have two Textboxes (TxtDriverName) And (TxtWeekNumber) When I select a Value from Combo Box (CmbxDriverName) The Driver Name get put into the (TxtDriverName) Then the Data Grid View shows all of the data for the selected Diver Name. Here Is where I want to Load the Combo Box (CmbxWeekNumber) to show the week Numbers related to the Driver Name only. The Code I have checks if the (TxtDriverName) is Null or Empty, And If empty go ahead and load All Week Numbers into the Combo Box (CmbxWeekNumber), Everything is working fine up to here Now, If I select A Driver Name from the Combo Box, The textbox (TxtDriverName) = Selected Driver Name from the combo Box I Then want to Load/Fill the Combo Box (CmbxWeekNumber) with only the selected Driver Name, with Week Number that Corresponds to the Driver Name Textbox (TxtDriverName) Can Anyone check my code to see if I have written the code right. Or Maybe I might have to split my code into two Subs. Basically everything will work on a Text Change Event in the Textboxes.


Private Sub LoadCmbxWeekNumber()

        'Query Database
        SQL.ExecQuery("SELECT * FROM MultiRecords")

        'check if any records exist, If no records exist, Then exit sub
        If SQL.RecordCount < 1 Then Exit Sub

        'check if Textbox (TxtDriverName.Text) Is Null or Empty
        If String.IsNullOrEmpty(TxtDriverName.Text) Then

            'if Textbox (TxtDriverName.Text) Is Null or Empty. Then Load Combo Box (CmbxWeekNumber)
            'with all Week Numbers In the Database
            SQL.ExecQuery("SELECT DISTINCT WeekNumber FROM MultiRecords ORDER BY WeekNumber ASC")

            For Each r As DataRow In SQL.SQLDT.Rows
                CmbxWeekNumber.Items.Add(r("WeekNumber"))
            Next


            'if Textbox (TxtDriverName) Is NOT Null or Empty. Then Load Combo Box (CmbxWeekNumber)
            'with all Week Numbers corresponding to the Textbox (TxtDriverName.Text)
        ElseIf Not String.IsNullOrEmpty(TxtDriverName.Text) Then

            'Add Parameter to Equal the Textbox (TxtDriverName)
            SQL.AddParam("@DriverName", TxtDriverName.Text)

            'Query Database For Week Number That = Driver Name (TxtDriverName.Text)
            SQL.ExecQuery("SELECT WeekNumber WHERE DriverName=@DriverName ORDER BY WeekNumber ASC")

            'Load Combo Box (CmbxWeekNumber) With Week Numbers, Where the drivers Name = (TxtDriverName.Text)
            For Each r As DataRow In SQL.SQLDT.Rows
                CmbxWeekNumber.Items.Add(r("WeekNumber"))
            Next

        End If

    End Sub

    Private Sub TxtDriverName_TextChanged(sender As Object, e As EventArgs) Handles TxtDriverName.TextChanged
        FindDriver()
        LoadCmbxWeekNumber()
    End Sub

Private Sub FindDriver()

        SQL.AddParam("@DriverName", TxtDriverName.Text)
        'Load The DataGridView ( DGVMultiRecords ) Where Driver Name = TxtDriverName
        LoadGrid("SELECT * FROM MultiRecords WHERE DriverName=@DriverName;")

    End Sub

Kind Regards Gary

Developer technologies | VB
{count} votes

Answer accepted by question author
  1. Jiachen Li-MSFT 34,231 Reputation points Microsoft External Staff
    2023-04-27T02:39:50.38+00:00

    Hi, Since you have already loaded the data into the datagridview, you can get the WeekNumber directly from the datagridview.

    You can refer to the code below.

                For Each str As String In (From row As DataGridViewRow In DGVMultiRecords.Rows
                                         Select row.Cells("WeekNumber").Value
                                         Distinct).ToList()
                    If Not String.IsNullOrEmpty(str) Then
                        CmbxWeekNumber.Items.Add(str)
                    End If
                Next
    

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.