show gridview header.

asp.net show  gridview header when no data or no recoards  in gridview. 

Here I explain how to display or show gridview header even if gridview does not contain data or show gridview header with no records or data.

aspx page-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Admin_Exam_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></title>
    <style type="text/css">
        .Gridview
        {}
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="gvdata" runat="server" CssClass="Gridview" GridLines="Both" 
            AutoGenerateColumns="false" HeaderStyle-BackColor="#123b6a" 
            HeaderStyle-ForeColor="White" Width="1006px">
<Columns>
 <asp:TemplateField HeaderText=" Id" >
                <ItemTemplate>

                    
                    <asp:Label ID="lblsljjno" runat="server" Text=' <%#Eval("store_id") %>'></asp:Label>
                    
              
                </ItemTemplate>
             <HeaderStyle  ForeColor="White"  CssClass="gridviewcourseheader"  />
            </asp:TemplateField>
             <asp:TemplateField HeaderText=" Name" >
                <ItemTemplate>

                    
                    <asp:Label ID="lblaaasno" runat="server" Text=' <%#Eval("store_name") %>'></asp:Label>
                    
              
                </ItemTemplate>
             <HeaderStyle  ForeColor="White"  CssClass="gridviewcourseheader"  />
            </asp:TemplateField>
            <asp:TemplateField HeaderText=" Code" >
                <ItemTemplate>

                    
                    <asp:Label ID="lbaswsflsno" runat="server" Text=' <%#Eval("store_code") %>'></asp:Label>
                    
              
                </ItemTemplate>
             <HeaderStyle  ForeColor="White"  CssClass="gridviewcourseheader"  />
            </asp:TemplateField>
            <asp:TemplateField HeaderText=" store_type_id" >
                <ItemTemplate>

                    
                    <asp:Label ID="ffff" runat="server" Text=' <%#Eval("store_type_id") %>'></asp:Label>
                    
              
                </ItemTemplate>
             <HeaderStyle  ForeColor="White"  CssClass="gridviewcourseheader"  />
            </asp:TemplateField>
</Columns>
</asp:GridView>
    
    </div>
    </form>
</body>

</html>


and c# code-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class Admin_Exam_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridviewData();
        }
    }
    protected void BindGridviewData()
    {
        SqlConnection con = new SqlConnection("Data Source=MANVENDRA;Initial Catalog=Newerp;Integrated Security=True");
        con.Open();
        SqlCommand cmd = new SqlCommand("select store_id,store_name,store_code,store_type_id from tbl_store", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        if (ds.Tables[0].Rows.Count == 0)
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            gvdata.DataSource = ds;
            gvdata.DataBind();
            int columncount = gvdata.Rows[0].Cells.Count;
            gvdata.Rows[0].Cells.Clear();
            gvdata.Rows[0].Cells.Add(new TableCell());
            gvdata.Rows[0].Cells[0].ColumnSpan = columncount;
            gvdata.Rows[0].Cells[0].Text = "No Records Found";
        }
        else
        {
            gvdata.DataSource = ds;
            gvdata.DataBind();
        }
        con.Close();

    }
}

Comments

Popular posts from this blog

Bootstrap Modal Popup keep open on PostBack in ASP.Net

Resolved Issue in Asp core 3.0 serializersettings does not exist in AddJsonOptions

.Net most asked interview questions for experienced professionals (C#,Asp WEBFORM,MVC,ASP CORE,WEB API,SQL Server,Java Script,Jquery)