Hi everyone,

I've created an application that runs queries against a SQL server (via Data Adaptors) in VB.NET 08 and returns the results to a DataGridView. It works great and returns my data, but when I switch to a different query that returns different columns, the DataGridView keeps the old columns from the previous query and adds the new columns from the new query to the end (far right) of the DataGridView. Is there a way to counter this? I don't want those previous unused columns showing up in my results.

I tried setting the DataGridView DataSource and DataMember to Nothing and that didn't fix it.

Here is my code...in case that helps

Connection.ConnectionString = "Provider=SQLOLEDB;Data Source=;Initial Catalog=;Integrated Security=SSPI;"
            Connection.Open()
            DataAdaptorVDN = New OleDb.OleDbDataAdapter(SQL, Connection)
            DataAdaptorVDN.Fill(DataSetVDN, "VDNResults")


            dgVDNResults.DataSource = DataSetVDN.DefaultViewManager
            dgVDNResults.DataMember = "VDNResults"
            dgVDNResults.AutoResizeColumns()
            dgVDNResults.AutoResizeRows()

Next time the button is clicked this is ran before refilling the datagridview

dgVDNResults.Columns.Clear()
            dgVDNResults.DataBindings.Clear()
            DataSetVDN.Clear()
            dgVDNResults.DataSource = Nothing
            dgVDNResults.DataMember = Nothing

>I tried setting the DataGridView DataSource and DataMember to Nothing and that didn't fix it.

dgVDNResults.Columns.Clear()
dgVDNResults.DataSource = Nothing

Show us event handler code.

>I tried setting the DataGridView DataSource and DataMember to Nothing and that didn't fix it.

dgVDNResults.Columns.Clear()
dgVDNResults.DataSource = Nothing

Show us event handler code.

Private Sub btnGetReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetReport.Click

I even tried another way of filling the DataGridView using a table/binding...Same exact issue

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.