Solved

How to rebuild and repopulate a dynamic gridview with different columns

Posted on 2011-03-10
5
582 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Authentication of Web Services 3 46
Interview questions for support of a legacy ASP.NET site 4 37
Allow User To Arrange Columns At Datagridview 3 19
Help with LINQ and XML 10 26
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

919 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

16 Experts available now in Live!

Get 1:1 Help Now