0

I am new to VB, and am struggling to figure out how to make this work. I have 3 databases being used on the back end that are populated by forms. I want to create the ability for the users to generate custom reports and export them to a .csv file.

There are roughly 20 values I want to provide options for in the combobox, and I can't find a way to better add these values than to manually enter each of them for all 20 rows (would make for a VERY long public sub) LOL. the below code works, but it won't scale. What can I use other than an IF statement so that the value selected in cmbColumn1 determines how the column is populated from the database?

Public Sub btnViewReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnViewReport.Click
    Dim OleDBC As New OleDbCommand
    Dim OleDBDR As OleDbDataReader
    Dim c As Integer
    c = 0
    If cmbColumn1.Text = "Patient ID" Then

        With OleDBC
            .Connection = conn
            .CommandText = "SELECT * FROM tblPatientsRecord WHERE PatientID like '%" & txtSearch.Text & "%'"
        End With
        OleDBDR = OleDBC.ExecuteReader
        DataGridViewReport.Rows.Clear()
        If OleDBDR.HasRows Then
            While OleDBDR.Read
                DataGridViewReport.Rows.Add()
                DataGridViewReport.Item(0, c).Value = OleDBDR.Item(0)

                c = c + 1
            End While
        Else
        End If
    End If
End Sub    
  • *"I have two problems"*. Then you need to ask two separate questions. Please edit your post to ask a single question, with all and only the information relevant to that question. If you have another question, post it separately with the information relevant to it. You can then accept an answer for each on independently. – jmcilhinney Nov 18 '19 at 00:10
  • edits made. thanks for the input. – peanutlemon Nov 18 '19 at 02:12
  • I don't really understand what you're asking for. Nothing in your code has anything to do with a `ComboBox` that I can see. I thought that your question was how to filter a grid column based on a `ComboBox` selection. Is that it? Do you want to populate a grid and then add the distinct values from a column into a `ComboBox` so that you can filter by that value? I suggest that you try again to provide a FULL and CLEAR explanation of exactly what you're trying to achieve, how you're trying to achieve it and what happens when you try. – jmcilhinney Nov 18 '19 at 02:50
  • What database are you using? – HardCode Nov 18 '19 at 21:59

1 Answers1

0
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
        Dim comboBoxHeaderCell1 As ComboBox = New ComboBox()
        comboBoxHeaderCell1.DropDownStyle = ComboBoxStyle.DropDownList
        comboBoxHeaderCell1.Visible = True
        comboBoxHeaderCell1.Items.Add("Column1")
        comboBoxHeaderCell1.Items.Add("Column2")
        dataGridView1.Controls.Add(comboBoxHeaderCell1)
        comboBoxHeaderCell1.Location = Me.dataGridView1.GetCellDisplayRectangle(0, -1, True).Location
        comboBoxHeaderCell1.Size = Me.dataGridView1.Columns(0).HeaderCell.Size
        comboBoxHeaderCell1.Text = "Column1"
    End Sub
nanda9894
  • 61
  • 5