Sorting gridview asp.net without overwriting data

I'm trying to make a gridview sortable that uses a stored procedure as a data source, I would not want it to rerun the query every time to achieve this. How do 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 is not postback the gridview has viewstate enabled. When the column headers are clicked, postbacks occur, but no sorting occurs. If anyone has a simple fix please let me know or even better ajax sort to avoid postbacks would be even better. The dataset is relatively small, however it takes a long time to ask why I would not want to demand from each species.

0


source to share


3 answers


If you're not peeking at the results and just reading, then something like the jquery tablesorter plugin would be a quick and easy solution. I've used this on tables up to 1400 rows and works great, although ~> a few hundred is probably better on slow puts.



If the gridview is editable, then aspnet event / input validation can spit on the dummy if you don't properly register client scripts, etc.

+4


source


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



0


source


In your case, I would use the SqlDataAdapter and populate the DataTable. Then put the DataTable in a session variable. When the GridView sorts, check if the Session variable is saved. If it is not, then fill the DataTable again. Finally, sort the DataTable with the DataView and reinstall the GridView with the DataView.

0


source







All Articles