We help IT Professionals succeed at work.

DataGridView column Headers won't refresh . . .

smo06
smo06 asked
on
221 Views
Last Modified: 2012-05-05
Hello,

I have a dataGridView (DGV) control in my project that is bound to a dataset.
Basically, the DGV displays search results, and the user has the ability to choose what fields they want displayed.
The problem is the DGV is remembering what columns were in the last search. I want to be able to clear all columns
and then display the columns from the Dataset every time the user clicks "Search".

dgv.columns.clear works to clear all column headings but won't display the headings from the nest search.

Here is a bit of the code . . .

private sub cmdSearch_click(byVal sender . . .)

                Dim da As New OracleClient.OracleDataAdapter(strSQL, oraConn)
            ds.Clear()
                dgv.columns.clear
            
            Try
                  da.Fill(ds, "recordSet")
                  
                  dgvSearchResults.DataSource = ds
                  dgvSearchResults.DataMember = "recordSet"
                  
                  tslSearchMessage.Text = ds.Tables("recordSet").Rows.Count & " Rows Returned."


            Catch ex As Exception
                  MessageBox.Show(ex.Message)

any ideas ?
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Did try dgvSearchResults.Refresh()?

Bob

Author

Commented:
I just tried it, I receive the same results.

In one instance my sql is returning 4 columns then a different sql statement returns 3 columns but the DGV still displays 4 columns.

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Does this show that you have the correct number of rows?

   tslSearchMessage.Text = ds.Tables("recordSet").Rows.Count & " Rows Returned."

Is AutoGenerateColumns = True?

Bob

Author

Commented:
The tslSearchMessage.text shows a correct number of returned records after each search and I'm afraid I don't see
an AutoGenerateColumns property for the DGV control. Is there a trick to access this property ?

Author

Commented:
I found the AutoGenerateColumn property and set it = TRUE during runtime. But again it does not eliminate the 4th column.

Author

Commented:
Thank you for your help but its quitin time.
Tomorrow is another day.

Thanks again,
Shawn
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Bob -
I see that you bind the grid to a DataTable ?
In my application I bind to a DataSet . . .

dim ds as dataset

me.DGV.datasource = ds
me.DGV.datamember = "recordSet"  ' - - - - - - "recordSet" being the name assigned to the table holding the results of the dataAdapter.fill


Is it better to use a dataTable ?

Shawn
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Shawn,

I you only have 1 DataTable, then it doesn't make any sense to have to overhead of the DataSet.  I makes it a little simpler, since you don't need to set DataMember, since the DataGridView is smart enough to know that the source is a single DataTable, instead of a DataSet (which can have multiple DataTable elements).

Bob

Author

Commented:
Thanks Bob
I was declaring my dataSet in the wrong place.
I'm now using a dataTable and declaring properly.

Thanks for your Help,

Shawn

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.