How does grid fix the column width?

Source: Internet
Author: User

Let alone:

1. Few fields:

The effect is as follows:

The width of each column is normal and displayed according to settings.

When there are many fields:

In order to display all fields on the screen, the sliding of the Set Div does not work.

Bind fields to the backendCode:

Boundfield BF = new boundfield (); <br/> BF. datafield = title_info [2, J]; <br/> BF. headertext = title_info [4, J]; <br/> BF. headerstyle. width = convert. toint32 (title_info [6, J]); <br/> BF. itemstyle. width = convert. toint32 (title_info [6, J]); <br/> BF. headerstyle. backcolor = system. drawing. color. lightsteelblue; <br/> // BF. headerstyle. forecolor = system. drawing. color. white; <br/> This. gridview1.columns. add (BF );

The front-end code is:

<Table id = "datalist_table" border = "1" width = "100%" cellpadding = "0"> <br/> <tr> <br/> <TD> <br/> <Div id = "datalist_div" style = "overflow: scroll; width: 97%; Border: 1; Height: 500px "> <br/> <asp: gridview id = "gridview1" runat = "server" autogeneratecolumns = "false" <br/> onrowdatabound = "gridview1_rowdatabound" ondatabound = "gridview1_databound"> <br/> </ASP: gridview> <br/> </div> <br/> </TD> <br/> </tr> <br/> </table>

What are you doing? How to set the fixed width of a column, even if there are many fields, then keep the width (sliding left and right ).

----------------------------------------------- Split line --------------------------------------------------------------

The solution is as follows:

1. Set the column width of the DIV to a fixed value rather than a percentage (when there are many columns, the percentage will be lifted)

<Div id = "datalist_div" style = "overflow: Scroll; width: 1000px; Border: 1; Height: 500px">

2. Set the column width of the gridview

This. gridview1.columns [clm_no]. itemstyle. width = convert. toint32 (title_info [6, J]);

This code is written after the gridview binds a column (gridview1.columns. Add (XXX.

3. In the databound event of the gridview, calculate and set the total width of the gridview:

Int sumwidth = 0; <br/> for (INT I = 0; I <gridview1.columns. count; I ++) <br/>{< br/> sumwidth = sumwidth + convert. toint32 (gridview1.columns [I]. itemstyle. width. value); </P> <p >}< br/> This. gridview1.width = sumwidth;

The horizontal slide is available so far. To enable the columns to wrap automatically when the width is small, we add this Code:

For (INT I = 0; I <E. row. cells. count; I ++) <br/>{< br/> E. row. cells [I]. style. add ("word-break", "Break-all"); <br/>}

Of course, you can also set the gridview. Attribute attribute directly in page_load or other places you need to achieve this effect. I won't write more here.

Field settings:

Effect:

We would like to thank csdner: wjq and other netizens for their answers.

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.