Solved

GridView Sorting

Posted on 2008-09-29
7
3,585 Views
Last Modified: 2013-11-08
Hi,

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
0
Comment
Question by:justin_smith
[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
  • 4
  • 3
7 Comments
 
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 ...like setting the data source etc...the code is also given below
        SearchControls();
 
        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";
            }
            else
            {
                e.SortDirection = SortDirection.Ascending;
                ViewState[e.SortExpression] = "Ascending";
                dvSort.Sort = e.SortExpression + " ASC";
            }
        }
        else
        { ViewState.Add(Convert.ToString(e.SortExpression), "Ascending"); dvSort.Sort = e.SortExpression + " ASC"; }
 
        grdView.DataSource = dvSort;
        grdView.DataBind();
 
 
    }
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

0
 

Author Comment

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

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



0
 
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

0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

Author Comment

by:justin_smith
ID: 22603187
this is what i get

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

Expert Comment

by:Jai S
ID: 22603208
Since you are assigning a SqlDataSource you probably might not be able to cast it directly...here is a link that might be helpful
http://www.developersdex.com/asp/message.asp?p=1116&r=6178158

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

Accepted Solution

by:
justin_smith earned 0 total points
ID: 22603224
Hi Ganesh,

works now....in 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;
                    gv_RevenueReport.DataBind();



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

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

635 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