Archive

Archive for January, 2016

GridView with Fixed Header and Scroll Bar

January 22, 2016 Leave a comment

Hi all,

Try this example to make GridView with Fixed Header and Scroll Bar

1- Open SQL Server and Create a new Database

2- Run the following Script to create “Districts” Table


CREATE TABLE [dbo].[Districts](
[DistID] [int] IDENTITY(100,10) NOT NULL,
[StateID] [int] NULL,
[DistName] [varchar](250) NULL,
[DistDesc] [varchar](max) NULL,
PRIMARY KEY CLUSTERED
(
[DistID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

3- Insert records into created Table

4- Open Visual Studio and create a new Website and add a new Web Page

5- From Toolbox add Gridview Control

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Fixed Header GridView</title>
<style type="text/css">
.FixedHeader {
position: absolute;
font-weight: bold;
vertical-align:text-bottom;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<h2 align="center">GridView with Fixed Header</h2>
<div style="height: 400px; overflow: auto" align="center">
<asp:GridView ID="gvDistricts" runat="server" HeaderStyle-CssClass="FixedHeader" HeaderStyle-BackColor="YellowGreen"
AutoGenerateColumns="false" AlternatingRowStyle-BackColor="WhiteSmoke" OnRowDataBound="gvDistricts_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="District ID" HeaderStyle-Width="80px" ItemStyle-Width="80px">
<ItemTemplate>
<asp:Label ID="lblDistID" runat="server" Text='<%#Eval("DistID")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField HeaderText="District Name" HeaderStyle-Width="120px" ItemStyle-Width="120px">
<ItemTemplate>
<asp:Label ID="lblDistName" runat="server" Text='<%#Eval("DistName")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<Columns>
<asp:TemplateField HeaderText="Description" HeaderStyle-Width="200px" ItemStyle-Width="200px">
<ItemTemplate>
<asp:Label ID="lblDistDesc" runat="server" Text='<%#Eval("DistDesc")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>

6- In Code behind add the following 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;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string ConStr = &amp;quot;ConnectionString here&amp;quot;;
if (!IsPostBack)
{
string Query = &amp;quot;SELECT * FROM Districts&amp;quot;;
SqlConnection con = new SqlConnection(ConStr);
SqlDataAdapter adp = new SqlDataAdapter(Query, con);
DataTable dt = new DataTable();
adp.Fill(dt);
gvDistricts.DataSource = dt.DefaultView;
gvDistricts.DataBind();
}
}

protected void gvDistricts_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowIndex == 0)
e.Row.Style.Add(&amp;quot;height&amp;quot;, &amp;quot;50px&amp;quot;);
}
}
}

Hope this helps

Good Luck

Advertisements
Categories: ASP.Net