views:

2327

answers:

3

I am trying to make a gridview sortable which uses a stored procedure as a datasource, I would not want it to rerun the query each time to achieve this. How would I get it to work my current code is:

protected override void OnPreRender(EventArgs e)
{
    if (!IsPostBack)
    {
    SqlCommand cmd2 = new SqlCommand("SR_Student_Course_List", new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["RDCV2ConnectionString"].ConnectionString));
    try
    {
        cmd2.CommandType = CommandType.StoredProcedure;
        cmd2.CommandTimeout = 120;
        cmd2.Parameters.Add("student_id", SqlDbType.Char, 11).Value = student;
        cmd2.Connection.Open();
        grdCourses.DataSource = cmd2.ExecuteReader();
        grdCourses.DataSourceID = string.Empty;
        grdCourses.DataBind();
    } finally
    {
        cmd2.Connection.Close();
        cmd2.Connection.Dispose();
        cmd2.Dispose();
    }}}

This code just binds the data when it isn't a postback, the gridview has viewstate enabled. On pressing the column headers a postback happens but no sorting occurs. If anyone has a simple fix for this please let me know or even better an ajax sort which would avoid the postback would be even better. The dataset is relatively small however takes a long time to query which is why I would not like to requery on each sort.

A: 

You could try storing the data in view state (or cache).

Robert Wagner
+4  A: 

If you are not paging the results, and just doing a read, then something like the jquery tablesorter plugin would be a quick and easy fix. I have used this on tables of up to 1400 rows and works great, although ~> few hundred probably better on slow putas.

If the gridview is editable, then aspnet event/input validation might spit a dummy if you don't go through the proper registration of client scripts etc.

seanb
Used the jquery table sorter works fantastic, my dataset output is only 20 lines max it's jsut a long time to compute it as it is aggregated data.
PeteT
A: 

In your case, I would use a SqlDataAdapter and fill a DataTable. Then, put the DataTable into a Session variable. When the GridView is sorting, check if the Session variable still exists. If it does not, then fill the DataTable again. Finally sort the DataTable using a DataView and rebind the GridView with the DataView.

CStick