Wednesday 5 March 2014

How to Create or Display multiple Header coloumn or multiple Header row and merge it with other columns in GridView in Asp.Net






Description:-

            In this Example we explain that how to Add or Display two Header Template or two Header row in Gridview in Asp.Net.generally gridview contain only one header and one footer row but sometime we need two display data with a two header.

For example:-

            Suppose we have display main two header like Employees data and office data and then in which there are four sub-header like Id and Name for Employees data and Salary and Year is the office data so at that time we must have to create a two header for grid view to separate the data in a meaning-full ways so end user can easily understand that when he/she show the data in gridview.   


To Show Example of How to Upload File in MVC Application then click here upload Image and bind to Gridview in MVC

To show Example of How to Upload Multiple File in MVC Application then click here upload multiple File in MVC

How to upload File and Fetch file from SqlServer and bind to Gridview fetch file from Sqlserver and bind to Gridview




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>Display Two Header Tempates or Header Row in Gridview</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="gvtwoheaderrow" runat="server"
                      BackColor="White"
                      BorderColor="Tan" BorderWidth="5px"
                      CellPadding="2" ForeColor="Black"
                      GridLines="None" BorderStyle="None" CellSpacing="2"
                      AutoGenerateColumns="False"
                      DataSourceID="SqlDataSource1"
                      OnRowCreated="gvtwoheaderrow_RowCreated" DataKeyNames="Id" EnableModelValidation="True">
              <FooterStyle BackColor="Tan" />
              <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
              <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue"
                      HorizontalAlign="Center" />
              <HeaderStyle BackColor="Tan" Font-Bold="True" />
              <AlternatingRowStyle BackColor="PaleGoldenrod" />
            <Columns>
                <asp:BoundField DataField="Id"
                                HeaderText="Id"
                                SortExpression="Id" InsertVisible="False" ReadOnly="True" />
                <asp:BoundField DataField="Name"
                                HeaderText="Name"
                                SortExpression="Name" />
                <asp:BoundField DataField="Salary"
                                HeaderText="Salary"
                                SortExpression="Salary" />
                <asp:BoundField DataField="Year"
                                HeaderText="Year"
                                SortExpression="Year" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString2 %>"
            SelectCommand="SELECT [Id], [Name], [Salary], [Year] FROM [poll]">
        </asp:SqlDataSource>
        &nbsp;</div>
    </form>
</body>
</html>



Default.aspx.cs:- 


using System;
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }
    //Code For Adding Header Row or Header Template
    protected void gvtwoheaderrow_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            GridView HeaderGridtemplate = (GridView)sender;
            GridViewRow HeaderGridRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
            TableCell HeaderCell = new TableCell();
            HeaderCell.Text = "Employee Details";
            HeaderCell.ColumnSpan = 2;
            HeaderGridRow.Cells.Add(HeaderCell);

            HeaderCell = new TableCell();
            HeaderCell.Text = "Office Details";
            HeaderCell.ColumnSpan = 2;
            HeaderGridRow.Cells.Add(HeaderCell);

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

        }
    }
}

 

0 comments:

Post a Comment