Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 605
  • Last Modified:

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.
0
thamilto0410
Asked:
thamilto0410
  • 3
  • 2
1 Solution
 
LIONKINGCommented:
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?
0
 
thamilto0410Author Commented:
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?
0
 
LIONKINGCommented:
If you have predefined columns for your gridview and then change the datasource at runtime, and the columns of your new datasource don't match the ones you defined (or created) at design time, then your grid will not display the data...

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.

Did you define your columns at design time?

Your solution might work, but you would have to add columns each and every time you change your query or want new data to be available. So I don't advise it...
0
 
thamilto0410Author Commented:
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.
0
 
LIONKINGCommented:
You're welcome... Glad I could help.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now