How to Sort custom data inside GridView TemplateField?

Last post 05-21-2009 4:36 PM by MinimalTech. 2 replies.

Sort Posts:

  • How to Sort custom data inside GridView TemplateField?

    05-14-2009, 5:56 AM
    • Member
      239 point Member
    • MinimalTech
    • Member since 01-19-2009, 7:22 AM
    • Greece
    • Posts 174

     Hello there!

    I have a problem with my GridView Control. In my aspx page, i have a GridView1 with columns:

     

    <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
    <asp:TemplateField HeaderText="CustomWord">
    <ItemTemplate>
    <asp:Label ID="lblWord" runat="server" Text=""></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
     

    The 'Name' column gets the data from the GridView1's SqlDataSource. Sorting is working well at that point.

    BUT,

    The 'lblWord' label of the 'CustomWord' column, gets the data from the custom-made function 'makeWord()' in the GridView1_RowDataBound() Event.

    The 'makeWord()' function generates a string by adding and reading different variables....

    HOW can i enable sort for the 'CustomWord' column? The GridView1's SqlDataSource does not have any relationship with 'makeWord()' function...

    The 'makeWord()' function does not includes any DataSource....

    Please, help! I have read a lot of articles inside and outside of forums.asp.net but i can't find the answer...

    Please, mark this post as "Answer" if it helped you solve your problem.

    MinimalTech.
  • Re: How to Sort custom data inside GridView TemplateField?

    05-19-2009, 12:27 AM
    Answer

    Hi MinimalTech ,

    Unfortunately, I assume that it may be impossible to sort against this field. From the personal perspective, the essntial factor is that this column is generated on the fly, that is to say data is formed by the time current row is databound. While the meaning of "sorting" is that re-arrange the order upon existing datasource, for instance: we can use DataView to sort data upon DataTable; we can sort data in table by using select statement with order by clause. How can we sort something upon empty datasource? 

    On the other hand, I concede that there still exsit at least one way to work this out, but the efficiency may be a little bit low. You can create DataTable in memeroy, add one additional column for holding the data which is calculated on the fly, bind the datatable to GV, and store the datatable in Session scope. By this way, you can sort that column by sorting the pertinent column of DataTable. It is very much like the description of this link: http://forums.asp.net/t/956540.aspx

     

     


    Samu Zhang
    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
  • Re: How to Sort custom data inside GridView TemplateField?

    05-21-2009, 4:36 PM
    • Member
      239 point Member
    • MinimalTech
    • Member since 01-19-2009, 7:22 AM
    • Greece
    • Posts 174

     Thank you very much Samu Zhang - MSFT!

    All you said are very useful and correct. I'll try to sort by saving the data into a DataTable in memory or in another SQL temporary table and

    if i stack anywhere, i'll tell you!

     

    P.S.: I have already read the 956540 article...

     

    Please, mark this post as "Answer" if it helped you solve your problem.

    MinimalTech.
Page 1 of 1 (3 items)