Solved

Asp.Net: sort a datagrid for a computed column

Posted on 2004-10-28
634 Views
Last Modified: 2012-05-05
Hello experts,
in my Asp.Net (vb.net) web application
I'm using in one web page the following definitions:
---
<asp:datagrid id="dgTest" ...>
...

<Columns>
      <asp:ButtonColumn Text="Close" ButtonType="PushButton" HeaderText="Close" CommandName="btnClose"></asp:ButtonColumn>
                              
      <asp:TemplateColumn HeaderText="Age/days" HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="15%"
                  ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                  <asp:Label id="lblAge" Text='<%# getAge(Container.DataItem("OrderDate")) %>' runat="server" />
                                     ^^^^^^ this field is my SORT field, it is not a field in the database
            </ItemTemplate>
      </asp:TemplateColumn>                              
                              
      <asp:TemplateColumn HeaderText="myKey" HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="15%"
                  ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
                  <asp:Label id="lblMyKey" Text='<%# DataBinder.Eval(Container.DataItem, "myKey") %>' runat="server" />
            </ItemTemplate>
      </asp:TemplateColumn>

      <asp:BoundColumn DataField="OrderDate" ReadOnly="True" HeaderText="OrderDate">
            <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
            <ItemStyle HorizontalAlign="Center" Width="10%"></ItemStyle>
      </asp:BoundColumn>
...
</Columns>
</asp:datagrid>
---
Now I want to sort the datagrid according the contents of the field lblAge.
The values of lblAge are computed using the public function getAge(...).
The result is a number of days (as string).
This all is working,
but I want to sort the datagrid depending on the contents of lblAge.

I tried to use OnSortCommand="sort4Age" in the datgarid tag
and defined an approriate routine in the code behind:
---
    Sub sort4Age(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs)
        ' ???
    End Sub
---
But the routine never gets control.

If anyone knows a solution please supply appropriate [snippet] information.

   Thank you very much!

     HStrix
0
Question by:HStrix
    13 Comments
     
    LVL 28

    Expert Comment

    by:mmarinov
    Hi HStrix,

    this can be happened if the binding of datagrid is not within

    if not ispostback then
        'bind grid
    end if

    Regards!
    B..M
    0
     

    Author Comment

    by:HStrix
    Thanks mmarinov,
    I call the routine for data binding in the click event of a button;
    it is not controlled by Page_Load.
    The datagrid is made visible after it is filled by the data binding routine.

    I'm also using
       OnItemCommand="showTestDetails"
    and the routine showTestDetails gets control after clicking an item.

      HStrix

    0
     
    LVL 28

    Expert Comment

    by:mmarinov
    HStrix,

    when you are saing "But the routine never gets control." do you mean that you have nevet go there or there is something else with code
    also the OnItemCommand is executed before the OnSortCommand and when you sort the grid you go in the OnItemCommand with CommandName="Sort"
    B..M
    0
     

    Author Comment

    by:HStrix
    Thanks mmarinov,
    I expected that 'sort4Age' gets activated during filling the datagrid.
    Using this routine is perhaps not the best way to solve the problem.

    But I hope that there is a better way to sort the datagrid during the filling process.
    I also tried to add 'AllowSorting="True"', but this doesn't seem to be everything what is needed.


    0
     
    LVL 10

    Expert Comment

    by:jnhorst
    Does this routine:

    Sub sort4Age(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs)

    end with Handles dgTest.SortCommand?  

    It should look like this:

    Sub sort4Age(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles dgTest.SortCommand

    End Sub

    John
    0
     

    Author Comment

    by:HStrix
    That was not the case,
    but adding the Handles information did not change anything.
    0
     
    LVL 28

    Expert Comment

    by:mmarinov
    the sorting is executed after the datagrid is databinded
    so you have to perform a custom sorting:
    1. get datasource
    2. add the value through getAge
    3. use the sort of dataview
    4. set the datasource to the sorted dataview
    5. bind the grid

    B..M
    0
     

    Author Comment

    by:HStrix
    Thanks mmarinov, I tried to follow your comment
    and modified my code as follows:
    ---
            objAdapter.SelectCommand = New System.Data.OleDb.OleDbCommand(strSQL, objConn)
            ' Fill the dataset.
            objAdapter.Fill(objDataset)
            ' Create a new view.
            Dim objView As New DataView(objDataset.Tables(0))
            ' Set up the data grid and bind the data.
            objView.Sort = "lblAge ASC"          ' here it crashes; cause: lblAge is not a field in the database table?
            dgTest.DataSource = objView
            dgTest.DataBind()                         ' during this process the routine getAge is called
    ---
    Something of my code must be incorrect.
    0
     
    LVL 28

    Expert Comment

    by:mmarinov
    that is the reason i've written
    2. add the value through getAge
    you have to manually add the column with modified data and then sorted by it
    in this case you there will be no need to use the getAge in the html but just render the new added column

    b..m
    0
     

    Author Comment

    by:HStrix
    Sorry mmarinov,
    I can't follow your suggestion.
    Can you supply any code to explain what you mean?

    HStrix
    0
     
    LVL 28

    Accepted Solution

    by:
    sorry for the delay, what i was mean :

            objAdapter.SelectCommand = New System.Data.OleDb.OleDbCommand(strSQL, objConn)
            ' Fill the dataset.
            objAdapter.Fill(objDataset)
            objDataSet.Tables(0).Columns.Add("getAge")

            for each dr as DataRow in objDataSet.Tables(0).Rows
                dr("getAge") = getAge(dr("OrderDate"))
            next

            ' Create a new view.
            Dim objView As New DataView(objDataset.Tables(0))
            ' Set up the data grid and bind the data.
            objView.Sort = "getAge ASC"          ' here it crashes; cause: lblAge is not a field in the database table?
            dgTest.DataSource = objView
            dgTest.DataBind()  

    but then you should change

    <asp:Label id="lblAge" Text='<%# getAge(Container.DataItem("OrderDate")) %>' runat="server" />
                                         ^^^^^^ this field is my SORT field, it is not a field in the database

    to

    <asp:Label id="lblAge" Text='<%# DataBinder.Eval(Container.DataItem, "getAge") %>' runat="server" />
                                         ^^^^^^ this field is my SORT field, it is not a field in the database


    B..M
    0
     

    Author Comment

    by:HStrix
    Thank you very much mmarinov,
    your solution is perfect!

      HStrix

    0
     
    LVL 28

    Expert Comment

    by:mmarinov
    glad that could help
    Regards!
    B..M
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
    User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    875 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

    7 Experts available now in Live!

    Get 1:1 Help Now