Link to home
Start Free TrialLog in
Avatar of AnandSahoo
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.OLEDB.4.0;" +
                "Data Source=C:\\dcs\\dcsslaalert.xls;" +
                "Extended Properties=Excel 8.0;";

            OleDbConnection objConn = new OleDbConnection(sConnectionString);

            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(objDataset1, "XLData");

            grid_Messlaalert.DataSource = objDataset1.Tables[0].DefaultView;
            grid_Messlaalert.DataBind();

              objConn.Close();
        }

dcsslaalert.xls
Avatar of gery128
gery128
Flag of India image

Do you have AutogenerateColumns = true? Because if you want to use default sorting of asp.net, you will need this to be true.

You can go through this link for ASP.NET sorting tutorial:
http://www.asp.net/data-access/tutorials/paging-and-sorting-report-data-cs

http://stackoverflow.com/questions/702600/sorting-and-paging-with-gridview-asp-net

http://www.exforsys.com/tutorials/asp.net-2.0/asp.net-2.0-adding-sorting-and-paging-in-gridview.html
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
Avatar of AnandSahoo
AnandSahoo

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.
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"].DefaultView.Sort = "ID ASC";    for example sort on ID column

Before

 grid_Messlaalert.DataSource = objDataset1.Tables[0].DefaultView;
            grid_Messlaalert.DataBind();

in your code.

Regards,

Dani
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.
ASKER CERTIFIED SOLUTION
Avatar of mcs0506
mcs0506
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Got th eidea to start working