GridView sort by a column whose header is dynamically created

Discussion in 'ASP .Net' started by gnewsgroup, Apr 11, 2008.

  1. gnewsgroup

    gnewsgroup Guest

    I have a GridView, in which the header of one column changes depending
    on the selected value of a DropDownList outside of this GridView.

    I did this dynamic header through

    protected void GridView1_RowCreated(object sender,
    GridViewRowEventArgs e)
    {
    if (e.Row.RowType == DataControlRowType.Header)
    {
    e.Row.Cells[5].Text =
    myDropDownList.SelectedValue;
    }
    }

    In the declaration of this GridView, I have

    AllowSorting="true"

    and of course I have a Sorting event handler.

    Every column is sortable except this column with the dynamic header.
    If assign a static value to this header and remove this RowCreated
    event handler, this column becomes sortable, too.

    So, the question is: How can I make the column with a dynamic header
    sortable?
     
    gnewsgroup, Apr 11, 2008
    #1
    1. Advertising

  2. It is essentially the same as sorting with a non-dynamically created header,
    except you have to control the sort. For a simple sort, you can use a
    DataView to bind and set up the sort there. With this form of sort, you can
    cache the data, most likely in ViewState, although there are applications
    where this is not acceptable.

    If you also need paging, you will have to write custom software to do this
    and will probably have to retrieve the data each time there is a sort so you
    can get a pointer to figure out start and end of the page.

    If you want to see how Microsoft does it, you can use Reflector on the .NET
    assemblies or, in VS 2008, step into the code. Both can give you a clue of
    how MS accomplishes sorting.

    Here's a couple of articles:
    http://lakshmik.blogspot.com/2006/06/aspnet-20-custom-paging-and-custom.html
    http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=428

    I am kind of fond of the idea in the second one, although I have not tried
    it. Binding with generics would take a bit of work to massage the data
    intitially, but the benefits outweigh the extra work.

    --
    Gregory A. Beamer
    MVP, MCP: +I, SE, SD, DBA

    Subscribe to my blog
    http://gregorybeamer.spaces.live.com/lists/feed.rss

    or just read it:
    http://gregorybeamer.spaces.live.com/

    *************************************************
    | Think outside the box!
    |
    *************************************************
    "gnewsgroup" <> wrote in message
    news:...
    >I have a GridView, in which the header of one column changes depending
    > on the selected value of a DropDownList outside of this GridView.
    >
    > I did this dynamic header through
    >
    > protected void GridView1_RowCreated(object sender,
    > GridViewRowEventArgs e)
    > {
    > if (e.Row.RowType == DataControlRowType.Header)
    > {
    > e.Row.Cells[5].Text =
    > myDropDownList.SelectedValue;
    > }
    > }
    >
    > In the declaration of this GridView, I have
    >
    > AllowSorting="true"
    >
    > and of course I have a Sorting event handler.
    >
    > Every column is sortable except this column with the dynamic header.
    > If assign a static value to this header and remove this RowCreated
    > event handler, this column becomes sortable, too.
    >
    > So, the question is: How can I make the column with a dynamic header
    > sortable?
     
    Cowboy \(Gregory A. Beamer\), Apr 11, 2008
    #2
    1. Advertising

  3. gnewsgroup

    gnewsgroup Guest

    On Apr 11, 9:55 am, "Cowboy \(Gregory A. Beamer\)"
    <> wrote:
    > It is essentially the same as sorting with a non-dynamically createdheader,
    > except you have to control the sort. For a simple sort, you can use a
    > DataView to bind and set up the sort there. With this form of sort, you can
    > cache the data, most likely in ViewState, although there are applications
    > where this is not acceptable.
    >
    > If you also need paging, you will have to write custom software to do this
    > and will probably have to retrieve the data each time there is a sort so you
    > can get a pointer to figure out start and end of the page.
    >
    > If you want to see how Microsoft does it, you can use Reflector on the .NET
    > assemblies or, in VS 2008, step into the code. Both can give you a clue of
    > how MS accomplishes sorting.
    >
    > Here's a couple of articles:http://lakshmik.blogspot.com/2006/0...r.spaanjaars.com/QuickDocId.aspx?quickdoc=428
    >
    > I am kind of fond of the idea in the second one, although I have not tried
    > it. Binding with generics would take a bit of work to massage the data
    > intitially, but the benefits outweigh the extra work.
    >
    > --
    > Gregory A. Beamer
    > MVP, MCP: +I, SE, SD, DBA
    >
    > Subscribe to my bloghttp://gregorybeamer.spaces.live.com/lists/feed.rss
    >
    > or just read it:http://gregorybeamer.spaces.live.com/
    >
    > *************************************************
    > | Think outside the box!
    > |
    > *************************************************"


    Thank you very much for the references. The problem for me right now
    is that the dynamically created header isn't even clickable, unlike
    other static header, which you can click and sort.
     
    gnewsgroup, Apr 18, 2008
    #3
  4. gnewsgroup

    AlphaQuam

    Joined:
    May 20, 2008
    Messages:
    1
    I ran into this same problem. Originally, I was using the Row.Cells collection as well. I would suggest trying the following...
    Code:
    GridView1.Columns[5].HeaderText = myDropDownList.SelectedValue;
    Be careful of when you make this assignment. I'm not sure what you're binding your GridView to, but changing the header text when using a LinqDataSource after its OnSelecting event (such as in the GridView's OnDataBinding event) will cause that event to fire again after the headers have been updated. Obviously, this could cause some serious performance issues, as well as other potential problems.
     
    AlphaQuam, May 20, 2008
    #4
  5. gnewsgroup

    bigal

    Joined:
    Jul 1, 2005
    Messages:
    1
    Location:
    Northern Virginia, USA
    It appears somebody fixed your problem AlphaQuam - of not being able to post. Please enable your private messages so people may contact you and welcome to the Velocity Reviews forums. :driver:
     
    bigal, May 21, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    5,116
  2. msimmons
    Replies:
    0
    Views:
    487
    msimmons
    Jul 16, 2009
  3. johnsonlim026
    Replies:
    0
    Views:
    936
    johnsonlim026
    Feb 27, 2010
  4. hansiman

    Image in header column (not replacing column header text)

    hansiman, Feb 5, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    3
    Views:
    309
    hansiman
    Feb 7, 2004
  5. Navin
    Replies:
    1
    Views:
    699
    Ken Schaefer
    Sep 9, 2003
Loading...

Share This Page