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.