0
votes

I make project have combobox items when i choose item from combobox it's view data in datagridview from the same table with Different columns The problem is when i choose item from combobox datagrid viewing data and when i choose anther item form combobox datagrid view new column with old columns

this is my code:

Dim sqlconn As New SqlConnection(con) 
Dim comm As New SqlCommand 
'Dim comm2 As New SqlCommand
Dim sql As String = "select * from entsab"
Dim adpt As New SqlDataAdapter(Sql, con)
Dim ds As New DataSet()
Dim dr As SqlDataReader 
Dim dr2 As SqlDataReader
Dim dt As New DataTable
Private Sub ComboBox2_SelectedValueChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedValueChanged, ComboBox3.SelectedValueChanged
    Select Case ComboBox2.SelectedItem
        Case ComboBox2.Items.Item(0) '1
            dt.Clear()
            Dim comm2 As New SqlCommand
            comm2.CommandText = Nothing
            DGV1.Refresh()
            comm2.CommandText = "select l1,sub_code,sub_name,houres from sub_with_sct where sction_ID=1"
            comm2.Connection = sqlconn
            sqlconn.Open()
            dr2 = comm2.ExecuteReader
            dt.Load(dr2)
            DGV1.AutoGenerateColumns = True
            DGV1.DataSource = dt
            DGV1.Refresh()
            sqlconn.Close()
            DGV1.Visible = True
            comm2.Dispose()
        Case ComboBox2.Items.Item(1)
            dt.Clear()
            Dim comm2 As New SqlCommand
            DGV1.DataSource = Nothing
            DGV1.Columns.Clear()
            comm2.CommandText = Nothing
            DGV1.Refresh()
            comm2.CommandText = "select l2,sub_code,sub_name,houres from sub_with_sct where sction_ID=1"
            comm2.Connection = sqlconn
            sqlconn.Open()
            dr2 = comm2.ExecuteReader
            dt.Load(dr2)
            DGV1.AutoGenerateColumns = True
            DGV1.DataSource = dt
            DGV1.Refresh()
            sqlconn.Close()
            DGV1.Visible = True
            comm2.Dispose()
    End Select
End Sub

When I choose first item:

enter image description here

When I choose second item:

enter image description here

1

1 Answers

0
votes

Try to add: DGV1.Columns.Clear() to your first case . and use dt= New DataTable() instead of dt.Clear() in your alls cases .