Use CSS styles to fix the table headers and specified columns of the DataGrid and GridView controls

Source: Internet
Author: User

Fixed Header:

Step 1: Add css code to the program, which can be saved as a separate file or referenced on the page.

The Css code is as follows:

. Fixed

{

Position: relative;

Table-layout: fixed;

Z-index: 10;

Top: expression (this. offsetParent. scrollTop );

}

. Fixed th

{

Text-overflow: ellipsis;

Overflow: hidden;

White-space: nowrap;

Padding: 2px;

}

Step 2: In the #. aspx. cs file background Page_LoadMediumReference Css code (you can also reference it in the head label of the #. aspx file. This is not described here)

The Code is as follows:

// ----- (Reference css in the subpage

HtmlLink CssControl = new HtmlLink ();

CssControl. Href = "~ /Css/Style.css "; // specify the path

CssControl. Attributes. Add ("rel", "stylesheet ");

Page. Header. Controls. Add (CssControl );

CssControl. Attributes. Add ("type", "text/css ");

//-----)))))

Step 3: reference

The Code is as follows:

<Div style = "overflow-y: scroll; width: 900px; height: 300px" id = "dvbody">

<Asp: DataGrid ID = "DataGrid1" runat = "server">

<Columns>

...

</Columns>

...

<HeaderStyle... CssClass = "Fixed"/>

</Asp: DataGrid>

</Div>

Note: overflow-y: scroll is displayed as a vertical scroll bar. To fix the header, you must specify a fixed height.

Reference Css (CssClass = "Fixed") in the DATAGRID (GRIDVIEW) Header style ").

Step 4: reference the following code in the

The Code is as follows:

<Meta http-equiv = "X-UA-Compatible" content = "IE = EmulateIE7">

Note: This line of code indicates rendering in IE7 display mode in IE8.

 

Fixed specified column:

Step 1: Add css code to the program, which can be saved as a separate file or referenced on the page.

The Css code is as follows:

. FixCol

{

Position: relative;

Table-layout: fixed;

Z-index: 10;

Left: expression (this. offsetParent. scrollLeft );

}

Step 2: In the #. aspx. cs file background Page_LoadMediumReference Css code (you can also reference it in the head label of the #. aspx file. This is not described here)

The Code is as follows:

// ----- (Reference css in the subpage

HtmlLink CssControl = new HtmlLink ();

CssControl. Href = "~ /Css/Style.css "; // specify the path

CssControl. Attributes. Add ("rel", "stylesheet ");

Page. Header. Controls. Add (CssControl );

CssControl. Attributes. Add ("type", "text/css ");

//-----)))))

Step 3: reference

The Code is as follows:

<Div style = "overflow-x: scroll; width: 900px; height: auto;" id = "dvbody">

<Asp: DataGrid ID = "DataGrid1" runat = "server">

<Columns>

<Asp: BoundColumn DataField = "" HeaderText = "" ReadOnly = "True">

<HeaderStyle CssClass = "FixCol"/>

<ItemStyle CssClass = "FixCol"/>

</Asp: BoundColumn>

...

</Columns>

...

</Asp: DataGrid>

</Div>

Note: overflow-x: scroll is displayed as a horizontal scroll bar. To fix the specified column, you must specify a fixed width.

Reference Css (CssClass = "FixCol") in the style of the DATAGRID (GRIDVIEW) column ").

Step 4: reference the following code in the

The Code is as follows:

<Meta http-equiv = "X-UA-Compatible" content = "IE = EmulateIE7">

Note: This line of code indicates rendering in IE7 display mode in IE8.

 

Note: Only one method is specified here. You need to make minor adjustments according to the actual situation during the implementation process.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.