[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

08/27/2007 at 11:15PM PDT, ID: 22790814
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.8

Dynamic gridview layout

Asked by softwarea in Programming for ASP.NET

Tags: gridview, dynamic

Hi experts,

I'm having a datasource that consists of customer data. To make it easy, assume there is one row per customer, inluding his name, address, last order, status and much more.

As there are quite a lot of fields, I need to provide my users with a grid view that supports customizable "layouts": The first four columns of this grid view are always the same: (1) checkbox, (2) image, (3) lastname, (4) firstname. I implemented these columns as TemplateFields during design time.
All other columns (column 5 - coIumn x) can be configured freely by my users. In other words: I let them choose (a) which columns they want to see and (b) in which order they want to see these columns.
By the way: the whole grid view is just for viewing and selection purposes, so there is no edit or delete functionality implemented.

So, this is what I'm doing if a user trys to change the gridview layout:

    private void ChangePoolLayout()
    {
        //1. Clear all custom columns (the first four columns must stay)
        for (int i = (gridView.Columns.Count - 1); i > 3; i--)
        {
            gridView.Columns.RemoveAt(i);
        }
        //2. Add custom columns in required sort order
        foreach (...)
        {
                //Create a new bound field
                BoundField newField = new BoundField();
                [...]
                //Add field to the columns collection of the grid view
                gridView.Columns.Add(newField);
         }
        //3. Rebind grid view
        gridView.DataSource = myDataSource;
        gridView.DataBind();
     }

HERE IS MY PROBLEM:
When I'm doing this the second time (second postback), the first four columns are still existent (showing the correct header captions), but they lost their ItemTemplates. Which means: the first four columns are suddenly *empty*.

I'm guesing this is a matter of the ViewState (which is set to true for the grid view) and/or dynamic control generation. So I tried to save the first columns to teh session on the very first time the page is called:

    void Page_Init(object sender, EventArgs e)
    {
        //Save first four columns to the session
        List<TemplateField> StandardColumns = new List<TemplateField>();
        if (Session["StdColumns"] == null)
        {
            foreach (TemplateField stdField in gridView.Columns)
            {
                    StandardColumns.Add(stdField);
            }
            Session["StdColumns"] = StandardColumns;
        }
     }

and then modified my procedure like this:

    private void ChangePoolLayout()
    {
        //1. Restore original layout (= 4 standard columns)
        //Clear ALL current columns
        gridView.Columns.Clear();
        //Read original layout from the session
        List<TemplateField> StandardColumns = (List<TemplateField>)Session["StdColumns"];
        //Rebuild original layout
        foreach (TemplateField stdField in StandardColumns)
        {
            gridView.Columns.Add(stdField);
        }
        //2. Add custom columns in required sort order
        foreach (...)
        {
                //Create a new bound field
                BoundField newField = new BoundField();
                [...]
                //Add field to the columns collection of the grid view
                gridView.Columns.Add(newField);
        }
        //3. Rebind grid view
        gridView.DataSource = myDataSource;
        gridView.DataBind();
     }

But here I'm running into "InvalidOperationException" on the DataBind(), saying that databinding methods like Eval(), XPath() and Bind() can only be used in the context of a databound control. To be honest, that does not give me any clue, because I checked with the debugger that the datasource is correct and even the grid columns are exactly as I expect them at this moment (including the ItemTemplates for the first four columns).

Well, I hope this was not too confusing AND that anybody can really help me out here.
Thanks so much!
[+][-]08/28/07 06:00 AM, ID: 19782467

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/28/07 07:22 AM, ID: 19783283

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08/28/07 08:23 AM, ID: 19783894

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/28/07 08:31 AM, ID: 19784006

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08/28/07 08:45 AM, ID: 19784151

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/28/07 08:51 AM, ID: 19784226

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08/28/07 08:54 AM, ID: 19784256

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/28/07 08:57 AM, ID: 19784299

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08/28/07 09:11 AM, ID: 19784425

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/28/07 09:15 AM, ID: 19784468

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08/28/07 09:23 AM, ID: 19784550

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]08/28/07 09:31 AM, ID: 19784625

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08/28/07 06:18 PM, ID: 19788052

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Programming for ASP.NET
Tags: gridview, dynamic
Sign Up Now!
Solution Provided By: TheLearnedOne
Participating Experts: 1
Solution Grade: A
 
 
[+][-]08/28/07 08:15 PM, ID: 19788461

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]08/29/07 05:52 AM, ID: 19790650

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20090824-EE-VQP-74 / EE_QW_1_20070628