?
Solved

Sort a gridview

Posted on 2014-02-24
6
Medium Priority
?
259 Views
Last Modified: 2014-02-26
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..
0
Comment
Question by:jcm4101
[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
  • 2
  • 2
  • 2
6 Comments
 
LVL 16

Expert Comment

by:SriVaddadi
ID: 39883941
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
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39883977
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

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.
0
 

Author Comment

by:jcm4101
ID: 39884928
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..
0
Create CentOS 7 Newton Packstack Running Keystone

A bug was filed against RDO for the installation of Keystone v3. This guide is designed to walk you through the configuration for using Keystone v3 with Packstack. You will accomplish this using various repos and the Answers file.

 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 750 total points
ID: 39885599
ObjectDataSource Strongly Typed Source Object Example
http://msdn.microsoft.com/en-us/library/ms227562(v=vs.100).aspx

In the example, you can see that the ObjectDataSource has properties that identify a method in a class to perform the CRUD operations.  These methods can perform any functions necessary to complete the operation, including complex steps of getting data from different sources.

<asp:ObjectDataSource
        ID="EmployeeDetailsObjectDataSource"
        runat="server"
        TypeName="Samples.AspNet.ObjectDataSource.NorthwindEmployeeData"
        DataObjectTypeName="Samples.AspNet.ObjectDataSource.NorthwindEmployee"
        SelectMethod="GetEmployee"
        InsertMethod="InsertEmployee"
        UpdateMethod="UpdateEmployee"
        DeleteMethod="DeleteEmployee"

        OnInserted="EmployeeDetailsObjectDataSource_OnInserted"
        OnUpdated="EmployeeDetailsObjectDataSource_OnUpdated"
        OnDeleted="EmployeeDetailsObjectDataSource_OnDeleted">
        <SelectParameters>
          <asp:Parameter Name="EmployeeID" Type="Int32" />  
        </SelectParameters>
      </asp:ObjectDataSource>
0
 
LVL 16

Accepted Solution

by:
SriVaddadi earned 750 total points
ID: 39885980
I can think of two approaches

1.  Write a database view which could pull data from different sources and use that view in your code

2. In the code, call different sources and populate a data table which you would use as a data source

In both the cases you could use sort expression to sort the data.
0
 

Author Closing Comment

by:jcm4101
ID: 39888188
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
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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

770 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