Solved

How to rebuild and repopulate a dynamic gridview with different columns

Posted on 2011-03-10
5
581 Views
Last Modified: 2012-05-11
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
Comment
Question by:thamilto0410
  • 3
  • 2
5 Comments
 
LVL 13

Expert Comment

by:LIONKING
ID: 35094896
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
 
LVL 1

Author Comment

by:thamilto0410
ID: 35111076
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
 
LVL 13

Accepted Solution

by:
LIONKING earned 350 total points
ID: 35111246
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
 
LVL 1

Author Closing Comment

by:thamilto0410
ID: 35111643
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
 
LVL 13

Expert Comment

by:LIONKING
ID: 35112424
You're welcome... Glad I could help.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now