二話(huà)不說(shuō)先上圖:
1.字段較少時(shí):
效果如下:
各列的寬度正常,都是按設置顯示的。
當字段較多時(shí):
為了能在屏幕上全部顯示出來(lái)各個(gè)字段都擠在了一塊,設置的DIV的滑動(dòng)不起作用。
后臺綁定各字段的代碼:
BoundField bf = new BoundField();
bf.DataField = title_info[2,j];
bf.HeaderText = title_info[4,j];
bf.HeaderStyle.Width =Convert.ToInt32(title_info[6,j]);
bf.ItemStyle.Width = Convert.ToInt32(title_info[6, j]);
bf.HeaderStyle.BackColor = System.Drawing.Color.LightSteelBlue;
//bf.HeaderStyle.ForeColor = System.Drawing.Color.White;
this.GridView1.Columns.Add(bf);
前臺的代碼為:
<table id ="datalist_table" border ="1" width ="100%" cellpadding ="0">
<tr >
<td>
<div id ="datalist_div" style="overflow:scroll;width:97%; border :1; height :500px">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns ="false"
onrowdatabound="GridView1_RowDataBound" ondatabound="GridView1_DataBound" >
</asp:GridView>
</div>
</td>
</tr>
</table>
請問(wèn)各位,你們又是如何處理的呢? 如何能設置列的固定寬度,即使在字段較多時(shí)任保持寬度(可左右滑動(dòng))。
-----------------------------------------------分割線(xiàn)--------------------------------------------------------------
問(wèn)題解決方法如下:
1.設置DIV的列寬為固定值而不是百分比(當列多時(shí)百分比會(huì )被撐開(kāi))
<div id ="datalist_div" style="overflow:scroll;width:1000px; border :1; height :500px">
2.設置GridView的各列寬
this.GridView1.Columns[clm_no].ItemStyle.Width = Convert.ToInt32(title_info[6, j]);
這段代碼寫(xiě)在GridView綁定了某列(GridView1.Columns.Add(XXX))之后。
3.在GridView的DataBound事件中計算并設置GridView的總寬度:
int sumWidth = 0;
for (int i = 0; i < GridView1.Columns.Count;i++ )
{
sumWidth = sumWidth + Convert.ToInt32(GridView1.Columns[i].ItemStyle.Width.Value);
}
this.GridView1.Width = sumWidth;
到目前為止水平的滑動(dòng)條已經(jīng)可用了。為了讓各列能在寬度設置較小情況下自動(dòng)換行我們加入這段代碼:
for (int i = 0; i < e.Row.Cells.Count; i++)
{
e.Row.Cells[i].Style.Add("word-break","break-all");
}
當然你也可以在Page_Load或其它需要的地方直接設置GridView.Attribute屬性來(lái)達到這一效果。這里就不多寫(xiě)了。
聯(lián)系客服