jcm4101
asked on
Sort a gridview
Hi
I have a gridview that sources its data from various sources..
For example...
The first two columns, Username and a date, are populated from a query that is coded inline...
Then, in the rowDataBound event for the gridview, I grab the username and perform further queries/Calculations using this username as a key to provide information I populate the rest of the columns with... (by modifying template fields in the gridview that hold label controls).
This all works fine, but I now am thinking that I might like to be able to sort this gridview on some of the fields that aren't populated by the inline query...
So, I was thinking that all I needed to do was pull the data out of the gridview into some sort of list or array, sort it there, then re-populate the gridview from this list/array...
Is this the right way to do this, or am I missing something obvious??
The issue is that the fields in the gridview aren't populated from the same query (and can't be), but I need a way of sorting the data and re-populating the gridview should the user wish to..
Any help would be appreciated..
thanks in advance..
I have a gridview that sources its data from various sources..
For example...
The first two columns, Username and a date, are populated from a query that is coded inline...
Then, in the rowDataBound event for the gridview, I grab the username and perform further queries/Calculations using this username as a key to provide information I populate the rest of the columns with... (by modifying template fields in the gridview that hold label controls).
This all works fine, but I now am thinking that I might like to be able to sort this gridview on some of the fields that aren't populated by the inline query...
So, I was thinking that all I needed to do was pull the data out of the gridview into some sort of list or array, sort it there, then re-populate the gridview from this list/array...
Is this the right way to do this, or am I missing something obvious??
The issue is that the fields in the gridview aren't populated from the same query (and can't be), but I need a way of sorting the data and re-populating the gridview should the user wish to..
Any help would be appreciated..
thanks in advance..
Not sure what are you using as DataSource but if it could be converted to DataView then you could use Sort expression on DataView and re-assign the sorted data source to grid view
If I was still working with ASP.NET Web Forms (and not MVC), then I would be choosing the ObjectDataSource, which can coordinate with the GridView to handling paging and sorting.
ObjectDataSource Class
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.objectdatasource.aspx
ObjectDataSource Class
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.objectdatasource.aspx
An ObjectDataSource control works with a class that you create. You create methods that retrieve and update data, and you provide the names of those methods to the ObjectDataSource control in markup. During rendering or postback processing, the ObjectDataSource calls the methods that you have specified.
ASKER
Thanks for the responses... Let me re-phrase the question, and approach the problem from a different angle....
I want to end up with a gridview that has several (40+) rows of data.
Each row pertains to an employee, so for example....
Column 1 = username (unique)
Column 2 = Accrued Leave Entitlement
Column 3 = Annual Salary
Column 4 = something else
Column 5 = something else again
etc.....
There are 13 columns in the actual table...
The information to populate each column comes from several different SQL data sources, so can't be queried for in a single query...
Is there a way of populating some sort of collection, keyed on the unique username, and then populate each of the "columns" for that user so that I can sort it on any particular column, then bind it to a gridview already sorted?
Thanks again for your help..
I want to end up with a gridview that has several (40+) rows of data.
Each row pertains to an employee, so for example....
Column 1 = username (unique)
Column 2 = Accrued Leave Entitlement
Column 3 = Annual Salary
Column 4 = something else
Column 5 = something else again
etc.....
There are 13 columns in the actual table...
The information to populate each column comes from several different SQL data sources, so can't be queried for in a single query...
Is there a way of populating some sort of collection, keyed on the unique username, and then populate each of the "columns" for that user so that I can sort it on any particular column, then bind it to a gridview already sorted?
Thanks again for your help..
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi..
I accepted both of these as the solution because I'm sure they were both correct, but actually only implemented the solution presented by SriVaddadi re the database view...
This fixed my issue and I learned something in the process (ie, database views!!) so was well pleased..
Thanks
D
I accepted both of these as the solution because I'm sure they were both correct, but actually only implemented the solution presented by SriVaddadi re the database view...
This fixed my issue and I learned something in the process (ie, database views!!) so was well pleased..
Thanks
D