Sunday, February 1, 2015

Grid view row span in ASP.NET

I know for some one it will sound like a very basic code for the row span of grid view but still there are lots of people that does not about that. So I thought it will be a good idea to write a blog post about it. So in this blog post we are going to learn How we can use row span in ASP.NET Grid Views.

So what we are waiting for Let’s take a small example of sample ASP.NET Web forms application. So Let’s start by creating an empty ASP.NET Webforms application like below.


After that I have added a page web page like below.

grid view row span in

And here is the code I have written for web page html.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestPage.aspx.cs" Inherits="GridViewRowSpan.TestPage" %>

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <asp:GridView ID="employeeGrid" runat="server" >
So here we  are going to create employee grid so I have created a employee class like following.
namespace GridViewRowSpan
    public class Employee
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Designation { get; set; }
        public string Department { get; set; }

And here’s how I have created a list of Employee class to demonstrate row span in web page_load event.

protected void Page_Load(object sender, EventArgs e)
    List<Employee> employees = new List<Employee>
        new Employee {Id=1,FirstName="Jalpesh",
            Designation="Project Manager",
        new Employee {Id=2,
            Designation="Software Developer",
        new Employee{Id=3,
            Designation="IT Manager",
            Department="IT Infrastructure"
        new Employee{Id=5,
            Designation="Network Admin",
            Department="IT Infrastructure"

    employeeGrid.DataSource = employees;

So If you see above code carefully. You can see that I have crated a list of employees where two employees have same last name and other thing you can see is employees are having common departments. If you see below code carefully then you can see that I have create a function called add AddRowSpanToGridView which add row span to columns of grid view. Following is a code for that.
public void AddRowSpanToGridView()
    for (int rowIndex = employeeGrid.Rows.Count - 2; rowIndex >= 0; rowIndex--)
        GridViewRow currentRow = employeeGrid.Rows[rowIndex];
        GridViewRow previousRow = employeeGrid.Rows[rowIndex + 1];

        for (int i = 0; i < currentRow.Cells.Count; i++)
            if (currentRow.Cells[i].Text == previousRow.Cells[i].Text)
                if (previousRow.Cells[i].RowSpan < 2)
                    currentRow.Cells[i].RowSpan = 2;
                    currentRow.Cells[i].RowSpan = previousRow.Cells[i].RowSpan + 1;
                previousRow.Cells[i].Visible = false;

If you see in the above code, What I have done is created a for loop that will traverse through rows from the bottom to top and then I have selected row and previous row via index and then again I have created a loop to traverse the cell(columns) and checked whether text of columns are same with previous row cell(column text). If both are same then I have checked with If that they have already have row span then I added one otherwise I have made row span to 2. Also I have made previous row cell visible false to display one column.

Now when you run application in browser. It will show row span as expected.


That’s it. Hope you like it. Stay tuned for more.

You can find complete of source code of this application on Github at -


  1. Thanks for sharing this informative blog. Recently I have completed Digital Marketing courses at a leading digital marketing company. It's really useful for me to make a bright career. If anyone wants to get Digital Marketing Course in Chennai visit infiniX located at Chennai. Rated as No.1 digital marketing company in Chennai.

  2. Thanks for sharing this informative blog. If anyone wants to get Unix Training in Chennai, Please visit Fita Academy located at Chennai, Velachery.

  3. Thanks for sharing this valuable information..If anyone wants to get SAP Training in Chennai, please visit FITA Academy located at Chennai..

  4. Thanks for sharing this informative blog. If anyone wants to get SAP Training in Chennai please refer this link.

  5. Your posts is really helpful for me.Thanks for your wonderful post.It is really very helpful for us and I have gathered some important information from this blog.If anyone wants to get Dot Net Training in Chennai reach FITA, rated as No.1 Dot Net Training Institutes in Chennai.

  6. Java is one of the popular technologies with improved job opportunity for hopeful professionals. Java Training in Chennai helps you to study this technology in details.

  7. Thanks
    for your informative article on digital marketing trends. I hardly stick with
    SEO techniques in boosting my online presence as its cost efficient and deliver
    long term results.

  8. Thanks for sharing this informative blog. Recently I did Digital Marketing courses in Chennai at a leading digital marketing company. It's really useful for me to make a bright career.


Your feedback is very important to me. Please provide your feedback via putting comments.

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog


My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews