Sorting Gridview data in

Posted on 2011-11-01
Last Modified: 2012-06-28
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);



            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;


Question by:AnandSahoo
    LVL 9

    Expert Comment

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

    You can go through this link for ASP.NET sorting tutorial:
    LVL 5

    Expert Comment

    you can sort DataTable first for axample as

    DataTable dt = new DataTable();
     //Define columns to DataTable

     //Adding rows to DataTable
     DataRow row = dt.NewRow();
     row["ID"] = 1;
     row["Name"] = "Jack";

     DataRow row1 = dt.NewRow();
     row1["ID"] = 2;
     row1["Name"] = "Fruit";

     // Sorting data based on ID
     dt.DefaultView.Sort = "ID ASC";

     GridView1.DataSource = dt;



    Author Comment

    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.

    Author Comment

    Also I have used dataset, How can I embed this in the code pasted by me above
    LVL 5

    Expert Comment

    you have to add following lines

    // Sorting data based on ID
     objDataset1.Tables["XLData"].DefaultView.Sort = "ID ASC";    for example sort on ID column


     grid_Messlaalert.DataSource = objDataset1.Tables[0].DefaultView;

    in your code.



    Author Comment

    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.
    LVL 5

    Accepted Solution


    Author Closing Comment

    Thanks Got th eidea to start working

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now