AnandSahoo
asked on
Sorting Gridview data in ASP.net
Dear Team,
Need one help on the below request. Started working in Visual studio .net to show some data in a webpage.(C#)
Attached one excel file from which I take the data and show it in grid_view on webpage.
Below is the code to access the excel data. I have enabled sorting as true in gridview properties so all the columns appearing as hyperlink.
Now I want to click on each column so that it will sort the data according to the column with ASC and DESC both.
Request you to help in this please.
using System;
using System.Collections.Generic ;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;
namespace DATACENTRE
{
public partial class Messaging : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
String sConnectionString = "Provider=Microsoft.Jet.OL EDB.4.0;" +
"Data Source=C:\\dcs\\dcsslaaler t.xls;" +
"Extended Properties=Excel 8.0;";
OleDbConnection objConn = new OleDbConnection(sConnectio nString);
objConn.Open();
worksheet.
OleDbCommand objCmdSelect = new OleDbCommand("Select * From [Sheet1$] where [GROUP] = 'MESSAGING TEAM'", objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;
DataSet objDataset1 = new DataSet();
objAdapter1.Fill(objDatase t1, "XLData");
grid_Messlaalert.DataSourc e = objDataset1.Tables[0].Defa ultView;
grid_Messlaalert.DataBind( );
objConn.Close();
}
dcsslaalert.xls
Need one help on the below request. Started working in Visual studio .net to show some data in a webpage.(C#)
Attached one excel file from which I take the data and show it in grid_view on webpage.
Below is the code to access the excel data. I have enabled sorting as true in gridview properties so all the columns appearing as hyperlink.
Now I want to click on each column so that it will sort the data according to the column with ASC and DESC both.
Request you to help in this please.
using System;
using System.Collections.Generic
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data;
namespace DATACENTRE
{
public partial class Messaging : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
String sConnectionString = "Provider=Microsoft.Jet.OL
"Data Source=C:\\dcs\\dcsslaaler
"Extended Properties=Excel 8.0;";
OleDbConnection objConn = new OleDbConnection(sConnectio
objConn.Open();
worksheet.
OleDbCommand objCmdSelect = new OleDbCommand("Select * From [Sheet1$] where [GROUP] = 'MESSAGING TEAM'", objConn);
OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
objAdapter1.SelectCommand = objCmdSelect;
DataSet objDataset1 = new DataSet();
objAdapter1.Fill(objDatase
grid_Messlaalert.DataSourc
grid_Messlaalert.DataBind(
objConn.Close();
}
dcsslaalert.xls
Hi
you can sort DataTable first for axample as
DataTable dt = new DataTable();
//Define columns to DataTable
dt.Columns.Add("Id");
dt.Columns.Add("Name");
//Adding rows to DataTable
DataRow row = dt.NewRow();
row["ID"] = 1;
row["Name"] = "Jack";
dt.Rows.Add(row);
DataRow row1 = dt.NewRow();
row1["ID"] = 2;
row1["Name"] = "Fruit";
dt.Rows.Add(row1);
// Sorting data based on ID
dt.DefaultView.Sort = "ID ASC";
GridView1.DataSource = dt;
GridView1.DataBind();
Regards,
Dani
you can sort DataTable first for axample as
DataTable dt = new DataTable();
//Define columns to DataTable
dt.Columns.Add("Id");
dt.Columns.Add("Name");
//Adding rows to DataTable
DataRow row = dt.NewRow();
row["ID"] = 1;
row["Name"] = "Jack";
dt.Rows.Add(row);
DataRow row1 = dt.NewRow();
row1["ID"] = 2;
row1["Name"] = "Fruit";
dt.Rows.Add(row1);
// Sorting data based on ID
dt.DefaultView.Sort = "ID ASC";
GridView1.DataSource = dt;
GridView1.DataBind();
Regards,
Dani
ASKER
Getting little confused with the codes given in the above links. In all these links columns have defined manually. In my kind I am taking the first row as coloumn header.
Hence not getting the show smart tag option for grid in design view.
Hence not getting the show smart tag option for grid in design view.
ASKER
Also I have used dataset, How can I embed this in the code pasted by me above
you have to add following lines
// Sorting data based on ID
objDataset1.Tables["XLData "].Default View.Sort = "ID ASC"; for example sort on ID column
Before
grid_Messlaalert.DataSourc e = objDataset1.Tables[0].Defa ultView;
grid_Messlaalert.DataBind( );
in your code.
Regards,
Dani
// Sorting data based on ID
objDataset1.Tables["XLData
Before
grid_Messlaalert.DataSourc
grid_Messlaalert.DataBind(
in your code.
Regards,
Dani
ASKER
Yes the above will give me the sorted data as per the column specified.
I should be able to click on each column and sort it as per that.
As per my code excel 2003 "HDR = Yes/No" is not specified and its taking the first row as column header. I have attached the sample excel database.
I should be able to click on SLA,TIME REMAINING etc and it should sort the data.
I should be able to click on each column and sort it as per that.
As per my code excel 2003 "HDR = Yes/No" is not specified and its taking the first row as column header. I have attached the sample excel database.
I should be able to click on SLA,TIME REMAINING etc and it should sort the data.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Got th eidea to start working
You can go through this link for ASP.NET sorting tutorial:
http://www.asp.net/data-ac
http://stackoverflow.com/q
http://www.exforsys.com/tu