Grid view with sort able column in asp.net

1. Create a asp.net project and on a .aspx file  copy and past below code.



<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="gridviewWithBoundField.aspx.cs" Inherits="SampleFileUpload.gridviewWithTempale" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" OnSorting="GridView1_Sorting" onrowdatabound="GridView1_RowDataBound" ShowHeaderWhenEmpty="true"
            AllowSorting="True" AutoGenerateColumns="False" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2" EnableModelValidation="True" GridLines="None" Width="315px" ForeColor="Black">
            <AlternatingRowStyle BackColor="PaleGoldenrod" />
            <Columns>
                <asp:BoundField DataField="StudentId" HeaderText="ID" ReadOnly="True"
                    SortExpression="StudentId" />
                <asp:BoundField DataField="StudentName" HeaderText="Name"
                    SortExpression="StudentName" />
                <asp:BoundField DataField="StuEdu" HeaderText="Education"
                    SortExpression="StuEdu" />
            </Columns>
            <FooterStyle BackColor="Tan" />
            <HeaderStyle BackColor="Tan" Font-Bold="True" />
            <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>

Note - If you remove SortExpression , then that column will not be sort able.

2.In code behind file add below code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace SampleFileUpload
{
    public partial class gridviewWithTempale : System.Web.UI.Page
    {
        DataTable dt = new DataTable();
        static string sortExp = "ASC";
        static SortDirection sortdirection = SortDirection.Descending;
       static GridViewRow headerRow;

        protected void Page_Load(object sender, EventArgs e)
        {
            GridView1.DataSource = BindGridviewData();
            GridView1.DataBind();
        }

        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            sortdirection = sortExp == "ASC" ? SortDirection.Ascending : SortDirection.Descending;
            sortExp = sortExp == "ASC" ? "DESC" : "ASC";
            dt.DefaultView.Sort = e.SortExpression + " " + sortExp;
            GridView1.DataSource = dt;
            GridView1.DataBind();
            Image sortImage = new Image();
            if (sortdirection == SortDirection.Ascending)
                 {
                   sortImage.ImageUrl = "~/Images/downword.jpg";
                   sortImage.AlternateText = "Ascending Order";
                  }
            else{

                sortImage.ImageUrl = "~/Images/upword.jpg";
                sortImage.AlternateText = "Descending Order";
            }
            int index = GetSortColumnIndex(e.SortExpression);
            headerRow.Cells[index].Controls.Add(sortImage);
        }
        int GetSortColumnIndex(string SortExpression)
        {
            foreach (DataControlField field in GridView1.Columns)
            {
                if (field.SortExpression == SortExpression)
                {
                    return GridView1.Columns.IndexOf(field);
                }
            }
            return -1;
        }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                headerRow = e.Row;
            }
        }
        private DataTable BindGridviewData()
        {
            dt.Columns.Add("StudentId", typeof(Int32));
            dt.Columns.Add("StudentName", typeof(string));
            dt.Columns.Add("StuEdu", typeof(string));
            dt.Rows.Add(1, "Suresh Dasari", "B.Tech");
            dt.Rows.Add(2, "Rohini Dasari", "Msc");
            dt.Rows.Add(3, "Madhav Sai", "MS");
            dt.Rows.Add(4, "Praveen", "B.Tech");
            dt.Rows.Add(6, "Sateesh", "MD");
            dt.Rows.Add(7, "Mahesh Dasari", "B.Tech");
            dt.Rows.Add(8, "Mahendra", "CA");
            return dt;
        }
    }
}

3. Add down arrow and up arrow to the image folder.




No comments:

Post a Comment