This article
will show you how you can bind gridview with datatable and short gridview data
in asp.net using c#.net.
Some of my
previous articles are as follows: Export
GridView Or Table Data Into PDF By C#.Net In Asp.Net Using jQuery, Reading
XML Document in C# Using Linq and Bind To GridView In Asp.Net, How
to Find Gridview Control on Button Click in Asp.Net C#, Nested
GridView Using c#.Net In Asp.Net, Read
XML File in Dataset And Bind To GridView In Asp.Net Using C#.Net, Bind
XML File Data to Gridview By Category and SubCategory in Asp.Net Using C#.Net, Hide
Or Show Gridview Column By Column Index at RunTime In Asp.Net Using C#.Net, Hide
Or Show Gridview Row By Column Name at RunTime In Asp.Net Using C#.Net, Display
Grand Total In Gridview Footer On RowDataBound In Asp.Net Using C#.Net.
So for this
article first we will create a new asp.net application and the gridview
control. After adding gridview control add the shorting event.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication4.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Short
GridView From Header Using C#.Net In Asp.Net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1"
runat="server" AllowSorting="True" OnSorting="GridView1_Sorting"></asp:GridView>
</div>
</form>
</body>
</html>
|
After this
we will add the shorting event
After
adding the above code add the below code.
using System;
using
System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using
System.Web.UI.WebControls;
namespace WebApplication4
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
if (!IsPostBack)
{
dt
= GetDataToBind();
GridView1.DataSource = dt;
GridView1.DataBind();
ViewState["GridData"] = dt;
}
}
/// <summary>
/// Grid View data
/// </summary>
/// <returns></returns>
public DataTable GetDataToBind()
{
DataTable dt = new DataTable("SaleData");
dt.Columns.Add(new DataColumn("Month", typeof(string)));
dt.Columns.Add(new DataColumn("Sale", typeof(int)));
DataRow dr1 =
dt.NewRow();
dr1["Month"] = "Group 1";
dr1["Sale"] =
10000;
dt.Rows.Add(dr1);
DataRow dr2 =
dt.NewRow();
dr2["Month"] = "Group 2";
dr2["Sale"] =
34300;
dt.Rows.Add(dr2);
DataRow dr3 =
dt.NewRow();
dr3["Month"] = "Group 3";
dr3["Sale"] =
23400;
dt.Rows.Add(dr3);
DataRow dr4 =
dt.NewRow();
dr4["Month"] = "Group 4";
dr4["Sale"] = 30040;
dt.Rows.Add(dr4);
DataRow dr5 =
dt.NewRow();
dr5["Month"] = "Group 5";
dr5["Sale"] =
342200;
dt.Rows.Add(dr5);
return dt;
}
/// <summary>
/// Shorting event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void
GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dt = ViewState["GridData"] as DataTable;
if (dt.Rows.Count > 0)
{
dt.DefaultView.Sort = e.SortExpression + " " +
SortingDirection(e.SortExpression);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
/// <summary>
/// Get shorting direction
/// </summary>
/// <param name="columnName"></param>
/// <returns></returns>
private string SortingDirection(string columnName)
{
string sortDirection = "ASC";
string sortExpression =
ViewState["ColumnName"] as string;
if (sortExpression != null)
{
if (sortExpression ==
columnName)
{
string lastDirection = ViewState["Direction"] as string;
if
((lastDirection != null) && (lastDirection == "ASC"))
{
sortDirection = "DESC";
}
}
}
ViewState["ColumnName"] = columnName;
ViewState["Direction"] = sortDirection;
return sortDirection;
}
}
}
|
In above code I have bid the gridview and after binding on shorting event of grid view I have detected the column name and detected what is the current status of the shorting.
0 comments:
Please let me know your view