How to sort Datatable in C#

Abstract


Just Drag and drop the Grid view control to web page.
[code]
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable table = new DataTable();
table.Columns.Add("Id", typeof(string));
table.Columns.Add("name", typeof(string));
table.Columns.Add("Offer", typeof(string));
table.Columns.Add("City", typeof(string));

DataRow row = null;
for (int i = 0; i < 5; i++)
{
row = table.NewRow();
row["Id"] = i;
row["name"] = "Ram";
row["Offer"] = "Offer";
row["City"] = "City Ram";

table.Rows.Add(row);
}
DataView dataView = new DataView(table);
dataView.Sort = "Id DESC";
//dataView.Sort = "Id DESC";
grvgrid.DataSource = dataView;
grvgrid.DataBind();

}
}



[/code]

No comments:

Post a Comment

Plz Share your comments...