How To Merge Gridview Header

During development with GridView, we might come across many situations in which we need to extend GridView for our requirements. For example, we need to have a separate header other than the header provided by GridView. In that case we need to add new GridView item of header type. In this post we will see how merge two or more columns.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MergeHeader.aspx.cs" Inherits="MergeHeader" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
   <title>Untitled Page</title>
</head>
<body>
   <form id="form1" runat="server">
       <div>
           <asp:GridView ID="GridView1" runat="Server" OnRowCreated="GridView1_RowCreated">
           </asp:GridView>
       </div>
   </form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class MergeHeader : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            GridView1.DataSource = CreateDataSource();
            GridView1.DataBind();
        }

    }
    ICollection CreateDataSource()
    {
        System.Data.DataTable dt = new System.Data.DataTable();
        System.Data.DataRow dr;
        dt.Columns.Add(new System.Data.DataColumn("Class", typeof(System.String)));
        dt.Columns.Add(new System.Data.DataColumn("Name", typeof(System.String)));
        dt.Columns.Add(new System.Data.DataColumn("Literature", typeof(System.Decimal)));
        dt.Columns.Add(new System.Data.DataColumn("Math", typeof(System.Decimal)));
        dt.Columns.Add(new System.Data.DataColumn("English", typeof(System.Decimal)));
        dt.Columns.Add(new System.Data.DataColumn("Computer", typeof(System.Decimal)));

        for (int i = 0; i < 8; i++)
        {
            System.Random rd = new System.Random(Environment.TickCount * i);
            dr = dt.NewRow();
            dr[0] = "Class" + i.ToString();
            dr[1] = "Student" + i.ToString();
            dr[2] = System.Math.Round(rd.NextDouble() * 100, 2);
            dr[3] = System.Math.Round(rd.NextDouble() * 100, 2);
            dr[4] = System.Math.Round(rd.NextDouble() * 100, 2);
            dr[5] = System.Math.Round(rd.NextDouble() * 100, 2);
            dt.Rows.Add(dr);
        }
        System.Data.DataView dv = new System.Data.DataView(dt);
        return dv;
    }

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            //Build custom header.
            GridView oGridView = (GridView)sender;
            GridViewRow oGridViewRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
            TableCell oTableCell = new TableCell();

            //Add Class
            oTableCell.Text = "Class";
            oGridViewRow.Cells.Add(oTableCell);

            //Add Student
            oTableCell = new TableCell();
            oTableCell.Text = "Student";
            oTableCell.BackColor = System.Drawing.Color.Fuchsia;
            oGridViewRow.Cells.Add(oTableCell);
            //Add marks
            oTableCell = new TableCell();
            oTableCell.Text = "Marks";
            oTableCell.ColumnSpan = 4;
            oTableCell.BackColor = System.Drawing.Color.Gray;
            oGridViewRow.Cells.Add(oTableCell);
            oGridView.Controls[0].Controls.AddAt(0, oGridViewRow);
        }

    }
}
Reactions

Post a Comment

0 Comments

Close Menu