Sorting and Template Columns

Discussion in 'ASP .Net Datagrid Control' started by Dave E, Oct 16, 2003.

  1. Dave E

    Dave E Guest

    I have a DataGrid set up and it works great. Because I need to modify the data on the fly and I don't know what columns I will have I create template columns programmatically adn add them to the datagrid. That works all fine and dandy. However I am now trying to add sorting and I cannot figure out how to assign the Sort function. I have created my template class from ITemplate and have the InstantiateIn function created. If I just add the Allowsorting and OnSortCommand to the definition in .aspx file it does not give me links the column headers. If I add them programmatically it also does not get me links in the column headers. I am assuming this problem is because I have the Template class that takes over the rendering of the items.

    My question is, how would I add this to the InstantiateIn function in the Template class? I do not have a clue. I have tried a variety of things but cannot seem to get it to work.
    Dave E, Oct 16, 2003
    #1
    1. Advertising

  2. Hi David,

    I have reviewed your issue. Due to the nature of your issue I need to do
    additional research to determine the best way to provide assistance. I will
    contact you as soon as possible.

    Best regards,

    Jacob Yang
    Microsoft Online Partner Support
    Get Secure! ¨C www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.
    Jacob Yang [MSFT], Oct 17, 2003
    #2
    1. Advertising

  3. Hi David,

    Based on my research, the problem comes from the SortExpression property on
    the Template Column, not the InstantiateIn function.

    If the SortExpression property on a template column was not set, the column
    header will not be rendered as LinkButton controls that raise the
    SortCommand event when clicked. You can experience this behavior with the
    VS.NET IDE. You can drag a DataGrid control on to a web page, enable the
    AllowSorting properties, and then add a template column with the Header
    Text into the DataGrid. If left the SortExpression empty, the Header Text
    of this template column will not be displayed as a link.

    Please refer to this URL for the sample code on how to set the
    SortExpression properties on a template column.

    http://www.superdotnet.com/Article.aspx?ArticleID=116

    Does it answer your question? If I have misunderstood your concern, please
    feel free to let me know.

    Best regards,

    Jacob Yang
    Microsoft Online Partner Support
    Get Secure! ¨C www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.
    Jacob Yang [MSFT], Oct 19, 2003
    #3
  4. Dave E

    Jim Nugent Guest

    "Dave E" <> wrote in message
    news:...
    > I have a DataGrid set up and it works great. Because I need to modify the

    data on the fly and I don't know what columns I will have I create template
    columns programmatically adn add them to the datagrid. That works all fine
    and dandy. However I am now trying to add sorting and I cannot figure out
    how to assign the Sort function. I have created my template class from
    ITemplate and have the InstantiateIn function created. If I just add the
    Allowsorting and OnSortCommand to the definition in .aspx file it does not
    give me links the column headers. If I add them programmatically it also
    does not get me links in the column headers. I am assuming this problem is
    because I have the Template class that takes over the rendering of the
    items.
    >
    > My question is, how would I add this to the InstantiateIn function in the

    Template class? I do not have a clue. I have tried a variety of things but
    cannot seem to get it to work.

    To get the linkbuttons, you need to set the sort expression for the column,
    something like this:

    DataGrid1.Columns[0].SortExpression = "LastName". This cause make your
    column headers into linkbuttons.

    The expression is what will be passed to your OnSortCommand() prodcudure as

    DataGridSortCommandEventArgs e.SortExpression when the user clicks on that
    link. Normally you would enter the sort expression for each column in the
    DataGrid property builder but since you are creating columns dynamically,
    you will have to set this property in the program.

    You also have to register a delegate to hand the SortCommand event:

    this.DataGrid1.SortCommand += new
    System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.DataGrid1_Sor
    tCommand);

    if it is not being done already. You can do it dymanically but since it is
    only necessary to do it once for the grid, It's probably easier to do it in
    design mode: in the Datagrid properties tab, select events (the lighning
    bolt) and next sort command select the member function to handle the event
    from the drop down. Then it add the code to register your function to the
    designer code.
    --
    Jim (for e-mail replace invalid with com)
    "Remember, an amateur built the Ark; professionals built the Titanic."
    Jim Nugent, Oct 26, 2003
    #4
    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. helpful sql
    Replies:
    0
    Views:
    797
    helpful sql
    May 19, 2005
  2. =?Utf-8?B?RG9ydGU=?=
    Replies:
    0
    Views:
    4,567
    =?Utf-8?B?RG9ydGU=?=
    Dec 14, 2005
  3. Replies:
    5
    Views:
    579
  4. Susil Patro

    Sorting using Dynamic Template Columns

    Susil Patro, Jun 1, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    117
    Susil Patro
    Jun 1, 2004
  5. Vinki

    template columns and bound columns in gridview

    Vinki, Apr 25, 2007, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    845
    Vinki
    Apr 25, 2007
Loading...

Share This Page