Sorting an asp.net gridview in ascending and descending order - Part 48

preview_player
Показать описание

Link for text version of this video

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

Please watch Part 47 using the link below, before proceeding with this video.

The problem in Part 47 was, we were not able to sort the data in descending order. In this video we will discuss about fixing this issue.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Demo
{
public class Employee
{
public int EmployeeId { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public string City { get; set; }
}

public class EmployeeDataAccessLayer
{
// Replace squre brackets with angular brackets
public static List[Employee] GetAllEmployees(string sortColumn)
{
// Replace squre brackets with angular brackets
List[Employee] listEmployees = new List[Employee]();

string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
string sqlQuery = "Select * from tblEmployee";

if (!string.IsNullOrEmpty(sortColumn))
{
sqlQuery += " order by " + sortColumn;
}

SqlCommand cmd = new SqlCommand(sqlQuery, con);

con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Employee employee = new Employee();
employee.EmployeeId = Convert.ToInt32(rdr["EmployeeId"]);
employee.Name = rdr["Name"].ToString();
employee.Gender = rdr["Gender"].ToString();
employee.City = rdr["City"].ToString();

listEmployees.Add(employee);
}
}

return listEmployees;
}
}
}

Step 3: Generate event handler method, for Sorting event of GridView1 control.

CurrentSortField="EmployeeId"
CurrentSortDirection="ASC"

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = EmployeeDataAccessLayer.GetAllEmployees("EmployeeId");
GridView1.DataBind();
}
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
//Response.Write("Sort Expression = " + e.SortExpression);
//Response.Write("Sort Direction = " + e.SortDirection.ToString());

SortDirection sortDirection = SortDirection.Ascending;
string sortField = string.Empty;

SortGridview((GridView)sender, e, out sortDirection, out sortField);
string strSortDirection = sortDirection == SortDirection.Ascending ? "ASC" : "DESC";

GridView1.DataSource = EmployeeDataAccessLayer.GetAllEmployees(e.SortExpression + " " + strSortDirection);
GridView1.DataBind();
}
private void SortGridview(GridView gridView, GridViewSortEventArgs e, out SortDirection sortDirection, out string sortField)
{
sortField = e.SortExpression;
sortDirection = e.SortDirection;

if (gridView.Attributes["CurrentSortField"] != null && gridView.Attributes["CurrentSortDirection"] != null)
{
if (sortField == gridView.Attributes["CurrentSortField"])
{
if (gridView.Attributes["CurrentSortDirection"] == "ASC")
{
sortDirection = SortDirection.Descending;
}
else
{
sortDirection = SortDirection.Ascending;
}
}

gridView.Attributes["CurrentSortField"] = sortField;
gridView.Attributes["CurrentSortDirection"] = (sortDirection == SortDirection.Ascending ? "ASC" : "DESC");
}
}
Рекомендации по теме
Комментарии
Автор

Thank you very much for taking time to give feedback. I am really glad you found these videos useful. If you want to receive email alerts, when new videos are uploaded, please feel free to subscribe to my youtube channel. May I ask you for a favour. I want these tutorials to be helpful for as many people as possible. Please free to share the link with your friends and family who you think would also benefit from them. If you like these videos, please click on the THUMBS UP button below the video

Csharp-video-tutorialsBlogspot
Автор

There are different ways to implement caching. You can cache the entire webform, or parts of webform (fragment caching) or just the application data, in this case the data to which the gridview control is binded. Please watch the videos on caching in asp.net video series, and you should be able to link both of these concepts. Hope this answers your question. Good Luck.

Csharp-video-tutorialsBlogspot
Автор

wow Too Good, i encountered the same issue yesterday and this video really helped ...thanks a lot venkat

xubair
Автор

Thanks love your presentation skill and your depth of knowledge... you helps me a lot.... Good Luck....and May God Bless you...

binishvbabu
Автор

Hi venkat, I am implemented the sorting according to video  but the custom property current sort direction getting change and always it is coming as ascending "asc" . Please help me on this.

Arvindkumar-wugj
Автор

i am waiting for this a lot....ur explanation is very good...

harikaaakutota
Автор

Ohhh Man God Bless you, Thanks a lot!!!

fadev
Автор

Hi Venkat, i have a small project where i am displaying all the file names from the folders and subfolders in a gridview not sql data, is it possible to sort them in grid view? if yes then how? i am beginner to the asp.net. could you please help me in this or share the link which you have already done example with this.

chidambarchat
Автор

Very helpful, but i want up down arrow with column name, how can i do that, plz make video on it.

gauravsingh
Автор

Thanks for the video Venkat... I didnt do exactly the way you did, but I used the idea ! Really thanks !

One doubt ! I'm using Paging AND Sorting... When I click to the second page and Sort it, it does not sort the content of the second page only, but the whole page, Why? Is there a way to work around it to sort only the content of the current page?

Ghaleon
Автор

Hello sir plz reply me this question. how to short data? if data bind on rowdatabound click event of grid view after calculation, and result will be display without click header text.

prashantchauhan
Автор

Do you know how to convert this to VB.net. I am using Visual Basics 2015 and have taken over a project from an employee who no longer works with us.

evrude
Автор

hi venkat,
i have one is used to improve performance...how to implement caching here....pls tell me .

harikaaakutota
Автор

How to I make, if data its content inside a arraylist? I am Beginner

andresmarincoto
Автор

Hi, thank you very much for taking time to give feedback. I am really glad you found these videos useful. To receive email alerts, when new videos are uploaded, please feel free to subscribe to my youtube channel. May I ask you for a favour. I want these tutorials to be helpful for as many people as possible. Please feel free to share the link with your friends and family who you think would also benefit from them. If you like these videos, please click on the THUMBS UP button below the video.

Csharp-video-tutorialsBlogspot