Link to home
Start Free TrialLog in
Avatar of HStrix
HStrix

asked on

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
Avatar of mmarinov
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
Avatar of HStrix

ASKER

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

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
Avatar of HStrix

ASKER

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.


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
Avatar of HStrix

ASKER

That was not the case,
but adding the Handles information did not change anything.
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
Avatar of HStrix

ASKER

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.
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
Avatar of HStrix

ASKER

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

HStrix
ASKER CERTIFIED SOLUTION
Avatar of mmarinov
mmarinov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of HStrix

ASKER

Thank you very much mmarinov,
your solution is perfect!

  HStrix

glad that could help
Regards!
B..M