Click here to Skip to main content
14,238,683 members

Group GridView Data

Rate this:
4.85 (55 votes)
Please Sign up or sign in to vote.
4.85 (55 votes)
22 Aug 2014CPOL
A custom GridView control which provides you an additional facility to group the data in gridview along with the facility to customise the Group Header and Group Footer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.OleDb;

// Written by Anurag Gandhi.
// Url:
// Contact me at:

/// <summary>
/// Summary description for ExcelLayer
/// </summary>
public class ExcelLayer
	public ExcelLayer()
		// TODO: Add constructor logic here
    /// <summary>
    /// Retireves the data from Excel Sheet to a DataTable.
    /// </summary>
    /// <param name="FileName">File Name along with path from the root folder.</param>
    /// <param name="TableName">Name of the Table of the Excel Sheet. Sheet1$ if no table.</param>
    /// <returns></returns>
    public static DataTable GetDataTable(string FileName, string TableName, string SortColumn)
            string strPath = HttpContext.Current.Request.PhysicalApplicationPath + FileName;
            DataSet ds = new DataSet();
            String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; " + "Data Source=" + strPath + "; " + "Extended Properties=Excel 8.0;";

            OleDbConnection objConn = new OleDbConnection(sConnectionString);

            string Query = "SELECT * FROM [" + TableName + "] where IsActive = 1";
            if (SortColumn != "")
                Query += " order by " + SortColumn;

            OleDbCommand objCmdSelect = new OleDbCommand(Query, objConn);
            OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();
            objAdapter1.SelectCommand = objCmdSelect;
            return ds.Tables[0];
        catch (Exception ex)
            //Log your exception here.//
            return (DataTable)null;

    public static DataTable GetDataTable(string FileName, string TableName)
        return GetDataTable(FileName, TableName, String.Empty);

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Anurag Gandhi
India India
Anurag Gandhi is a Developer, Consultant, Architect, Blogger, Speaker and a Microsoft Employee. He is passionate about programming.
He is extensively involved in Asp.Net Core, MVC/Web API, Microsoft Azure/Cloud, web application hosting/architecture, Angular, AngularJs, design and development. His languages of choice are C#, Node/Express, JavaScript, Asp .NET MVC, Asp, C, C++. He is familiar with many other programming languages as well. He mostly works with MS Sql Server as the preferred database and has worked with Redis, MySQL, Oracle, MS Access, etc. also.
He is active in programming communities and loves to share the knowledge with others whenever he gets the time for it.
He is also a passionate chess player.
Linked in Profile:
He can be contacted at:

Disclaimer: Any posts, article, content present in this website is my personal opinion based on best of my knowledge and experience. None of the content provided by me should be considered as official communication/opinion or my Employer.


135 bookmarked