GridView固定标题溢出容器

时间:2016-07-12 10:57:57

标签: c# html css asp.net gridview

我在Div容器中有一个带有冻结标题的gridview。当gridview中的标题列数不超过容器时,它工作正常,如果我在gridview中添加更多标题列,它会溢出div容器。

enter image description here

    <%@ 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代码 /

 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();

1 个答案:

答案 0 :(得分:1)

请删除HeaderStyle-Width="x"的所有出现,并且不再会产生不良影响。