Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Tagged as

Gridview Sorting using attribute [Source Code]

, 30 Aug 2013 CPOL
Sorting GridView using attribute property of GridView
demo.zip
demo of my project
App_Code
App_Data
database
database.bak
Thumbs.db
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default2 : System.Web.UI.Page
{
    Bal objGetAll = new Bal();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            getrecord();
        }
    }
    protected void getrecord()
    {
       
        try
        {
            DataTable dtAll = new DataTable();
            dtAll = objGetAll.select();
            if (dtAll.Rows.Count > 0)
            {
                ViewState["dtAll"] = dtAll;
                GridView1.DataSource = dtAll;
                GridView1.DataBind();

                //btnSubmit.Visible = true;
                //btnCancel.Visible = true;
            }
            else
            {
                lblmsg.Text = "There are no records found.";
                GridView1.DataSource = null;
                GridView1.DataBind();
                //btnSubmit.Visible = false;
                //btnCancel.Visible = false;
            }
        }
        catch (Exception ex)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "script", "<script text='text/javascript'> alert('" + ex.Message + "')</script>");
        }
    }


    protected void gvcontent_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataTable dtAll = new DataTable();
        dtAll = objGetAll.select();
        DataTable dt = dtAll.Copy();
        DataView dv = new DataView(dt);
        string sort = getDirection(e.SortDirection.ToString());
        dv.Sort = e.SortExpression + " " + sort;
        GridView1.DataSource = dv;
        GridView1.DataBind();
    }
    private string getDirection(String col)
    {
        string sortby = "";
        if (col == "Ascending")
        {
            if (Convert.ToString(GridView1.Attributes["current_sortdirection"]) != "")
            {

                if (Convert.ToString(GridView1.Attributes["current_sortdirection"]) == "asc")
                {
                    sortby = "Desc";
                    GridView1.Attributes["current_sortdirection"] = "desc";
                }
                else
                {
                    sortby = "asc";
                    GridView1.Attributes["current_sortdirection"] = "asc";
                }

            }
            else
            {
                sortby = "Desc";
                GridView1.Attributes["current_sortdirection"] = "Desc";
            }
        }
        else
        { 
            
        }
         
        return sortby;
    }


    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
   

    


  
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }
    
}

       
    


  


  
    
    

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

AJD-salute
Web Developer
India India
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 31 Aug 2013
Article Copyright 2013 by AJD-salute
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid