Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Sorting Gridview data in ASP.net

Posted on 2011-11-01
8
Medium Priority
?
259 Views
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);

            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
0
Comment
Question by:AnandSahoo
  • 4
  • 3
8 Comments
 
LVL 9

Expert Comment

by:gery128
ID: 37062191
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
0
 
LVL 5

Expert Comment

by:mcs0506
ID: 37062376
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
0
 

Author Comment

by:AnandSahoo
ID: 37062456
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.
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 

Author Comment

by:AnandSahoo
ID: 37062510
Also I have used dataset, How can I embed this in the code pasted by me above
0
 
LVL 5

Expert Comment

by:mcs0506
ID: 37062544
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
0
 

Author Comment

by:AnandSahoo
ID: 37062675
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.
0
 
LVL 5

Accepted Solution

by:
mcs0506 earned 2000 total points
ID: 37062726
0
 

Author Closing Comment

by:AnandSahoo
ID: 37119076
Thanks Got th eidea to start working
0

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month15 days, 23 hours left to enroll

581 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