GridView Sorting

Posted on 2008-09-29
Last Modified: 2013-11-08

I am using SqlDataSource to bind data to Gridview....

i am forced to determined the query only dynamically..

so it is all done programatically...

I am having in issue with implementing a handler for sorting..

Can someone help me with a handler that works with SqlDataSource as the DataSource for GridView....

Also how can we get a DataTable from SqlDataSource
Question by:justin_smith
  • 4
  • 3
LVL 14

Expert Comment

by:Jai S
ID: 22603159
below is a code that i use for sorting...let me know if you need further help

DataTable dtViewData = new DataTable();
//this is the Sorting event of the grid view(data grid)...bind this even to you data grid...
    protected void grdView_Sorting(Object sender, GridViewSortEventArgs e)
//this procedure call is to load you data grid setting the data source etc...the code is also given below
        DataView dvSort = new DataView();
        dvSort = dtViewData.DefaultView;
//dtViewData is your data table - which in this case is loaded in teh function SearchControls
        string str = Convert.ToString(e.SortDirection);
        e.SortDirection = SortDirection.Ascending;
        ViewState.Add("SortCol", Convert.ToString(e.SortExpression));
        if (ViewState[e.SortExpression] != null)
            if (Convert.ToString(ViewState[e.SortExpression]) == "Ascending")
                e.SortDirection = SortDirection.Descending;
                ViewState[e.SortExpression] = "Desending";
                dvSort.Sort = e.SortExpression + " DESC";
                e.SortDirection = SortDirection.Ascending;
                ViewState[e.SortExpression] = "Ascending";
                dvSort.Sort = e.SortExpression + " ASC";
        { ViewState.Add(Convert.ToString(e.SortExpression), "Ascending"); dvSort.Sort = e.SortExpression + " ASC"; }
        grdView.DataSource = dvSort;
private void SearchControls(string sort)
        dtViewData = new DataTable();
        ssql = "your dynamic sql code goes here...";
        dtViewData = objDb.GetDataTable(CommandType.Text, ssql);

Open in new window


Author Comment

ID: 22603173
I will have to implement this way if nothing works..(cos i have to rewrite what i have do so far)

I am with you in use of DataView.

please let me know if this can be done..

1)Can i get DataView / DataTable object from GridView.DataSource
2)I am pretty sure this could be easily done if i initially used DataTable instead of SqlDataSource object

LVL 14

Expert Comment

by:Jai S
ID: 22603180
i think you can...

use it this way....

DataTable dt = (DataTable)YourGridViewName.DataSource;

Open in new window

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 22603187
this is what i get

Unable to cast object of type 'System.Web.UI.WebControls.SqlDataSource' to type 'System.Data.DataTable'.
LVL 14

Expert Comment

by:Jai S
ID: 22603208
Since you are assigning a SqlDataSource you probably might not be able to cast it is a link that might be helpful

if you can share your code i will look at it...

Accepted Solution

justin_smith earned 0 total points
ID: 22603224
Hi Ganesh,

works case someone wants to a helping hand.....

  SqlDataSource  ds= (SqlDataSource)  gv_RevenueReport.DataSource;
        DataView view = (DataView)ds.Select(new DataSourceSelectArguments());

                    view.Sort = e.SortExpression + " " +" DESC";

                    gv_RevenueReport.DataSource = view;

LVL 14

Expert Comment

by:Jai S
ID: 22603235
i am also using dynamic loading of a gridview wherein my query changes for each screen and my internal datagrid loading dynamically...i managed to achieve the loading time (15 records per view) in less than  a second for tables with more than a 100 thousand records....

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Suggested Solutions

Title # Comments Views Activity
Name Space error VS2015 1 35
custom authorization on controller action and HTML in mvc 1 42
IDE for Python 5 62 Moving Visual Studio 2015 project to 2017 2 30
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

685 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