![](/img/trans.png)
[英]Unable to set Width of Column in GridView ASP.NET (No DataSource for GridView)
[英]ASP.NET Set GridView Column max width size
我在VS2005上使用ASP.NET C#。
我有一個GridView表,其中包含一個名為Description
的列,由於輸入總是很長,因此水平描述非常長。
我希望GridView具有所有列的最大寬度大小。
我嘗試了很多方法,但沒有一個方法奏效。
ItemStyle-Width="50px"
,
HeaderStyle-BorderWidth="50px"
,
HeaderStyle-Width="50px"
,
RowStyle-Width="50px"
,
Width="50px"
下面是我的GridView的代碼片段:
<asp:GridView ID="GridView1" AutoGenerateEditButton="True" ondatabound="gv_DataBound" runat="server" DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="UserSelector" OnCheckedChanged="UserSelector_CheckedChanged" ondatabound="gv_DataBound" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
任何人都知道如何根據我的情況調整GridView列的大小?
這就是我控制列寬的方法。
1.在標題中添加CSS。
<style type="text/css">
.maxWidthGrid
{
max-width: 300px;
overflow: hidden;
}
</style>
2.然后在必要的列中使用CSS,例如ItemStyle-CssClass="maxWidthGrid"
<asp:BoundField ItemStyle-CssClass="maxWidthGrid" DataField="ClientName" HeaderText="Client Name"
ReadOnly="True" SortExpression="ClientName" />
我改變了我的代碼是你在哪里思考回合?
<RowStyle Width="150px"/>
刪除所有藏匿處
<asp:TemplateField><ItemTemplate> </ItemTemplate></asp:TemplateField>
並且只是使用簡單
<columns> </columns>
在這里,我認為你的代碼看起來像AutoGenerateColumns =“True”或false假裝不重要
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" DataSourceID="SqlDataSource1">
<Columns>
<asp:CheckBox ID="UserSelector" OnCheckedChanged="UserSelector_CheckedChanged" ondatabound="gv_DataBound" runat="server" />
</Columns>
<RowStyle Width="150px"/>
</asp:GridView>
你可以處理RowDataBound
事件。
protected int widestData;
protected void GridView1_RowDataBound(object sender,
GridViewRowEventArgs e)
{
System.Data.DataRowView drv;
drv = (System.Data.DataRowView)e.Row.DataItem;
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (drv != null)
{
String catName = drv[1].ToString();
Response.Write(catName + "/");
int catNameLen = catName.Length;
if (catNameLen > widestData)
{
widestData = catNameLen;
GridView1.Columns[2].ItemStyle.Width =
widestData * 30;
GridView1.Columns[2].ItemStyle.Wrap = false;
}
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
widestData = 0;
}
請查看如何:動態設置GridView Web服務器控件列寬以獲取更多詳細信息。
如果您的gridview autogeneratecolumns
屬性設置為false並且您正在創建custom columns
則可以使用templatefileds
,您可以在其中實現Table
和Div
來控制列的寬度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.