Latest

random

C# - Merging GridView Header Columns/Cells

C# - GridView column header merging in ASP.NET



In previous example I explained How to bind ArrayList element to GridView in C#? In this post I will explain how to merge or group Gridview Header columns/cells using OnRowCreated event in asp.net. Find the source code below :-


how to group, how to merge, column header,merging,aspnet gridview,OnRowCreated merge columns,gridview column,gridview header,column group,aspnet,net grid view,aspnet, asp control

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

<!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>C# - Merging GridView Header Columns/Cells</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="grvMergeHeader" runat="server" AutoGenerateColumns="False" OnRowCreated="grvMergeHeader_RowCreated">
            <Columns>
                <asp:BoundField DataField="EmpId" HeaderText="Employee Id" />
                <asp:BoundField DataField="Name" HeaderText="Employee Name" />
                <asp:BoundField DataField="Address" HeaderText="Employee Address" />
                <asp:BoundField DataField="DateTime" HeaderText="DateTime" />
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("EmpId", typeof(int));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Address", typeof(string));
            dt.Columns.Add("DateTime", typeof(DateTime));

            //
            // Here we add five DataRows.
            //
            dt.Rows.Add(25, "Rk", "Gurgaon", "2/11/2013 9:00:00 AM");
            dt.Rows.Add(50, "Sachin", "Noida", "4/18/2013 9:00:00 AM");
            dt.Rows.Add(10, "Nitin", "Noida", "8/20/2013 9:00:00 AM");
            dt.Rows.Add(21, "Aditya", "Meerut", "2/13/2013 9:00:00 AM");
            dt.Rows.Add(100, "Mohan", "Banglore", "1/09/2013 9:00:00 AM");

            grvMergeHeader.DataSource = dt;
            grvMergeHeader.DataBind();
        }
    }
    protected void grvMergeHeader_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            GridView HeaderGrid = (GridView)sender;
            GridViewRow HeaderGridRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
            TableCell HeaderCell = new TableCell();
            HeaderCell.Text = "Employee Information";
            HeaderCell.ColumnSpan = 3;
            HeaderGridRow.Cells.Add(HeaderCell);

            HeaderCell = new TableCell();
            HeaderCell.Text = "Joining Date";
            HeaderCell.ColumnSpan = 2;
            HeaderGridRow.Cells.Add(HeaderCell);

            grvMergeHeader.Controls[0].Controls.AddAt(0, HeaderGridRow);

        }
    }
Download Source Code Click here  ..



No comments:

All Rights Reserved by Etechpulse © 2015 - 2016
Powered By Blogger

Contact Form

Name

Email *

Message *

Powered by Blogger.