Avatar of thamilto0410
thamilto0410

asked on 

How to rebuild and repopulate a dynamic gridview with different columns

Good Morning,
I need an example or links to an example of a solution for the following:
I have a VB.NET application with an oracle backend.  This application has the ability to build 7 different reports.  The report chosen is databound to a gridview with autogenerate columns set to false.  What I need is when the codebehind is called for any of the reports the gridview dumps its content rebuilds the columns and rebinds with a new report.  I need it to dump as the user has the ability to call any one of the reports and one report might have 3 columns and the next report might have 6 columns.  They choose the report from a dropdown, select a date range and click GET and the report drops into the gridview and can be viewed, exported to excel or printed.

I have tried leaving the presentation blank except for a panel and building the gridview in the codebehind as well as dropping the gridview onto the aspx in design and setting generate columns to false and then adding the columns in the codebehind and using clear columns.  But then the problem is my grid either adds the columns twice or if I use removeat I have had index must be nonnegative error.  

I am new to developing in .net and just need some guidance as to the best way to work this out.

I cannot use other means of developing reports like Crystal Reports as even the most experienced developer here has not been able to get it working yet in this environment.

Any assistance whether an example or a link to an example will be greatly appreciated.

Thanks.
Visual Basic.NETASP.NET

Avatar of undefined
Last Comment
Member_2_861731
Avatar of Member_2_861731
Member_2_861731
Flag of Canada image

Have you tried setting the datasource to nothing and binding the gridview so it displays no data at all, then setting the data source to your new query and then rebinding?
Avatar of thamilto0410
thamilto0410

ASKER

LIONKING -- That did not work but I think I know what I am doing wrong.  I will tell you and you advise please.  I think it is because I have a case statement that executes the results based on the report title chosen but  I am adding the columns for each report without knowing how to dispose of prior built columns.
Tell me if this will fix my problem?  What if I on page load in my if not page.ispostback I created and added  all of the columns I need regardless of the report and then in the case statement based on report title just hid the columns not needed.  Would this work?
ASKER CERTIFIED SOLUTION
Avatar of Member_2_861731
Member_2_861731
Flag of Canada image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of thamilto0410
thamilto0410

ASKER

THANK YOU!!  That was exactly the nudge in the direction I needed.  I did as you suggested (If you don't define any columns at design time, once you set the data source at runtime the gridview will automatically create all the columns that are returned by your query, allowing you to hide/show them at will.)  and it is working beautifully now.
You're welcome... Glad I could help.
ASP.NET
ASP.NET

The successor to Active Server Pages, ASP.NET websites utilize the .NET framework to produce dynamic, data and content-driven web applications and services. ASP.NET code can be written using any .NET supported language. As of 2009, ASP.NET can also apply the Model-View-Controller (MVC) pattern to web applications

128K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo