Fixed header of grid view on scrolling in asp.net c#

Many times, we want to fixed header of grid view for showing convenient values on scrolling. Instead we can use paging properties of Gridview, but in this article we are learn about how to fix header in Gridview on scrolling
Database:-



Source Code:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="fixedheadergridview.aspx.cs" Inherits="fixedheadergridview" %>

<!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_header_table
     {

         background:#ffffff;
         color:#fafafa;
         font-weight:bold;
         border:1px solid ##f08a0b;
         width:580px;
     }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <div class="gridview_header_table">
        <table bgcolor="#f08a0b" rules="all" >
            <tr>
                <td style ="width:109px;">Name</td>
                <td style ="width:167px;">Contact Number</td>
                <td style ="width:294px;">Email</td>
            </tr>
        </table>
        </div>
        <div style ="height:130px; width:600px; overflow:auto;">
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            ShowHeader = "False" Width ="580px" >
                <Columns>
                    <asp:BoundField DataField="Name" />
                    <asp:BoundField DataField="Contact_no" />
                    <asp:BoundField DataField="Email" />
                </Columns>
            </asp:GridView>
            </div>
    </div>
    </form>
</body>
</html>


Code behind (c#):-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

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

    protected void bindgridview()
    {


        SqlConnection con = new SqlConnection("Data Source=NEERAJ-PC;Initial Catalog=CodeSolution;Persist Security Info=True;User ID=sa; password=12345678");
        con.Open();
        SqlCommand cmd = new SqlCommand("Select * from record", con);
        SqlDataAdapter add = new SqlDataAdapter(cmd);
        DataSet da = new DataSet();
        add.Fill(da);
        GridView1.DataSource = da;
        GridView1.DataBind();
    }
}


Out-Put:-
Fixed header of grid view on scrolling in asp.net c# Fixed header of grid view on scrolling in asp.net c# Reviewed by NEERAJ SRIVASTAVA on 8:54:00 PM Rating: 5

No comments:

Powered by Blogger.