Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Asp.Net: sort a datagrid for a computed column

Posted on 2004-10-28
13
Medium Priority
?
638 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
Comment
Question by:HStrix
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
13 Comments
 
LVL 28

Expert Comment

by:mmarinov
ID: 12441575
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
ID: 12441668
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
ID: 12441683
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:HStrix
ID: 12441768
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
ID: 12441778
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
ID: 12441838
That was not the case,
but adding the Handles information did not change anything.
0
 
LVL 28

Expert Comment

by:mmarinov
ID: 12441872
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
ID: 12441953
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
ID: 12442246
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
ID: 12442870
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:
mmarinov earned 2000 total points
ID: 12453611
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
ID: 12458398
Thank you very much mmarinov,
your solution is perfect!

  HStrix

0
 
LVL 28

Expert Comment

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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

618 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