Asp.Net: sort a datagrid for a computed column

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
HStrixAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mmarinovCommented:
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
HStrixAuthor Commented:
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
mmarinovCommented:
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
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

HStrixAuthor Commented:
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
jnhorstCommented:
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
HStrixAuthor Commented:
That was not the case,
but adding the Handles information did not change anything.
0
mmarinovCommented:
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
HStrixAuthor Commented:
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
mmarinovCommented:
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
HStrixAuthor Commented:
Sorry mmarinov,
I can't follow your suggestion.
Can you supply any code to explain what you mean?

HStrix
0
mmarinovCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HStrixAuthor Commented:
Thank you very much mmarinov,
your solution is perfect!

  HStrix

0
mmarinovCommented:
glad that could help
Regards!
B..M
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.