I have a gridview with a frozen header in a Div container. It works perfectly when the number of header columns in the gridview does not exceed the container's width, but if I add more header columns, it overflows the div container.
https://i.sstatic.net/TwKfG.jpg
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test2.aspx.cs" Inherits="Test2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.header { position:absolute; }
</style>
</head>
<body>
<form id="form1" runat="server">
<div style="overflow:scroll; height:250px; width:600px;" >
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
CellPadding="4" ForeColor="#333333" Width="600px">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" HeaderStyle-Width="271px" />
<asp:BoundField DataField="Branch" HeaderText="Branch" HeaderStyle-Width="91px" />
<asp:BoundField DataField="City" HeaderText="City" HeaderStyle-Width="194px" />
<asp:BoundField DataField="Contact" HeaderText="NewColumn1" HeaderStyle-Width="194px" />
<asp:BoundField DataField="Email" HeaderText="NewColumn2" HeaderStyle-Width="194px" />
</Columns>
<HeaderStyle CssClass="header" BackColor="#7961da"
Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
</asp:GridView>
</div>
</form>
</body>
</html>
/Aspx.cs Code/
protected void Page_Load(object sender, EventArgs e)
{
DataTable table = new DataTable();
table.Columns.Add("Name");
table.Columns.Add("Branch");
table.Columns.Add("City");
table.Columns.Add("Contact");
table.Columns.Add("Email");
for (int i = 0; i < 80; i++)
{
DataRow row1 = table.NewRow();
List<string> report1 = new List<string>();
report1.Add("XYZ");
report1.Add("ABC");
report1.Add("PQR");
row1["Name"] = "MyName";
row1["Branch"] = "MyBrach";
row1["City"] = "London";
row1["Contact"] = "NewColumnData1";
row1["Email"] = "NewColumnData2";
table.Rows.Add(row1);
}
GridView1.DataSource = table;
GridView1.DataBind();