ASP.Net GridView Sorting using Header Columns in C# and VB.Net

PRA
 
on Feb 02, 2021 03:31 AM
743 Views

Hi!

How I can do sort by header like this Ascending Descending Sorting using Columns Header in ASP.Net GridView for below code?

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

public partial class mainForm : System.Web.UI.Page
{
    string UserData = System.Configuration.ConfigurationManager.AppSettings.Get("Base");//connection

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Grid.DataSource = peoplesName(Convert.ToInt32(1));
        Grid.DataBind();
    }

    public List<object> peoplesName(int min)
    {
        SqlConnection con = new SqlConnection(UserData);
        SqlCommand cmd = con.CreateCommand();
        List<object> people = new List<object>();
        cmd.CommandText = "select id, name, age from inform where region = " + min;
        con.Open();
        try
        {
            SqlDataReader rdr = cmd.ExecuteReader();
            if (rdr != null)
            {
                while (rdr.Read())
                {
                    people.Add(new
                    {
                        id = rdr.GetInt32(0),
                        name = rdr.GetString(1),
                        age = rdr.GetInt32(2)
                    });
                }
            }
        }
        catch { };
        con.Close();
        return people;
    }
}

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="mainForm.aspx.cs" Inherits="mainForm" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Button ID="btnSearch" runat="server" OnClick="btnSearch_Click" Text="Search" />
            <br />
            <asp:GridView ID="Grid" runat="server" AutoGenerateColumns="False" AutoGenerateDeleteButton="False" AutoGenerateEditButton="False" BackColor="White" BorderColor="Black" BorderStyle="Double" BorderWidth="2px" CellPadding="3" CellSpacing="1" DataKeyNames="id" HorizontalAlign="Center" style="margin-left: 0px;" Width="998px">
                <AlternatingRowStyle Wrap="True" />
                <Columns>
                    <asp:TemplateField HeaderText="Select">
                        <ItemTemplate>
                            <asp:CheckBox ID="chkSelect" runat="server" />
                        </ItemTemplate>
                        <ControlStyle Width="30px" />
                        <FooterStyle Width="30px" Wrap="False" />
                        <HeaderStyle Width="30px" Wrap="False" />
                        <ItemStyle Width="30px" Wrap="False" />
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Id">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" AutoComplete="off" Text='<%# Bind("id") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("id") %>'></asp:Label>
                        </ItemTemplate>
                        <ControlStyle Width="30px" />
                        <FooterStyle Width="30px" Wrap="False" />
                        <HeaderStyle Width="30px" Wrap="False" />
                        <ItemStyle Width="30px" Wrap="False" />
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Name">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox2" runat="server" AutoComplete="off" Text='<%# Bind("name") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label2" runat="server" Text='<%# Bind("name") %>'></asp:Label>
                        </ItemTemplate>
                        <ControlStyle Width="160px" />
                        <FooterStyle Width="160px" Wrap="False" />
                        <HeaderStyle Width="160px" Wrap="False" />
                        <ItemStyle Width="160px" Wrap="False" />
                    </asp:TemplateField>                    
                    <asp:TemplateField HeaderText="Age">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox4" runat="server" AutoComplete="off" ReadOnly="True" Text='<%# Bind("age") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label4" runat="server" Text='<%# Bind("age") %>'></asp:Label>
                        </ItemTemplate>
                        <ControlStyle Width="30px" />
                        <FooterStyle Width="30px" Wrap="False" />
                        <HeaderStyle Width="30px" Wrap="False" />
                        <ItemStyle Width="30px" Wrap="False" />
                    </asp:TemplateField>
                </Columns>
                <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
                <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
                <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
                <RowStyle BackColor="#DEDFDE" ForeColor="Black" />
                <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
                <SortedAscendingCellStyle BackColor="#F1F1F1" />
                <SortedAscendingHeaderStyle BackColor="#594B9C" />
                <SortedDescendingCellStyle BackColor="#CAC9C9" />
                <SortedDescendingHeaderStyle BackColor="#33276A" />
            </asp:GridView>
        </div>
    </form>
</body>
</html>

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download