Thursday, March 29, 2012

Griview Sorting with Bound Fields in C#.Net


declare static variable before page load event

statis int nSort =0;

write below code in gridview sorting event

protected void gvSearch_Sorting(object sender, GridViewSortEventArgs e)
    {
        try
        {
                //sorting Grid For all Records
                DataTable dt = dsFG.Tables[0];--------->fill dataset or datatable from database
                DataView dv = new DataView(dt);
                if (nSort % 2 == 0)
                {
                    dv.Sort = e.SortExpression + " " + "ASC";
                }
                else
                {
                    dv.Sort = e.SortExpression + " " + "DESC";
                }
                nSort++;
                gvSearch.DataSource = dv;
                gvSearch.DataBind();        
        }
        catch (Exception ex)
        {
            lblmessage.Text = ex.Message;
        }
        finally
        {
        }
    }