Friday, February 24, 2012

GridView Show Footer And Header No Record in Grid

When No Record in Grid view at that time show Header and Footer.
only we add on Row in Dataset.

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

<!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>GridView Show Footer And Header No Record in Grid</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>
<a href="http://aspdotnet-example.blogspot.com/">http://aspdotnet-example.blogspot.com
</a>
</h1>
<h1>GridView Show Footer And Header No Record in Grid</h1>
<asp:GridView ID="gridView" runat="server" AutoGenerateColumns="false" DataKeyNames="MetalId"
EmptyDataText="No Data Found." PageSize="10" CellPadding="4" ForeColor="#333333"
GridLines="None" Style="margin-right: 0px" AllowPaging="True">
<RowStyle BackColor="#EFF3FB" />
<Columns>
<asp:BoundField DataField="MetalName" HeaderText="GridView Name" ReadOnly="True"
SortExpression="MetalName" />
<asp:BoundField DataField="Status" ItemStyle-HorizontalAlign="Center"
 HeaderText="Is GridView Active?"
ReadOnly="True" SortExpression="Status" />
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</div>
</form>
</body>
</html>
GridViewNoRecord.aspx.cs 
using System;
using System.Linq;
using System.Web.UI.WebControls;
using System.Data;

public partial class GridViewNoRecord : System.Web.UI.Page

{

    LogicLayer objLogicLayer = new LogicLayer();

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!Page.IsPostBack)
        {

            FillGridView();

        }

    }

    protected void FillGridView()

    {

        if (objLogicLayer.ListMetals().Tables[0].Rows.Count != 0)

        {

            gridView.DataSource = objLogicLayer.ListMetals();

            gridView.DataBind();

        }

        else

        {

            BuildNoRecords(gridView, objLogicLayer.ListMetals());

        }

    }
     public void BuildNoRecords(GridView gridView, DataSet ds)
    {

        try

        {

            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());

            ds.Tables[0].Rows[0][1] = "No Records Found.";

            gridView.DataSource = ds;

            gridView.DataBind();
}

        catch (Exception ex)

        {

        }

    }

}
GridView Show Footer And Header No Record in Grid
ASPdotNET-Example