在GridView的数据绑定列的ASP.NET设置宽度
我有一个使用绑定列列的一个GridView。我想设置为我的的UserInfo
列了maxWidth。
I have a GridView which uses BoundField for columns. I am trying to set a maxwidth for my UserInfo
column.
我试过很多方法很多,但其中的非正常工作。下面是code为我的 GridView控件的:
I have tried many many ways but non of them work. Below is the code for my GridView :
<asp:GridView ID="GridView1" AutoGenerateEditButton="True"
ondatabound="gv_DataBound" runat="server" DataSourceID="SqlDataSource1"
AutoGenerateColumns="False">
<Columns>
<asp:BoundField HeaderText="UserId"
DataField="UserId"
SortExpression="UserId"></asp:BoundField>
<asp:BoundField HeaderText="Username"
DataField="Username"
SortExpression="Username"></asp:BoundField>
<asp:BoundField HeaderText="UserInfo"
DataField="UserInfo"
SortExpression="UserInfo"></asp:BoundField>
</Columns>
</asp:GridView>
寻找建议我如何可以设置特定的列,这是我的的UserInfo
列的宽度。
我为你做一个小的演示。演示如何显示长文本。
I did a small demo for you. Demonstrating how to display long text.
在这个例子中有一栏名称可以包含很长的文本。在的BoundField 将显示在表格单元格的所有内容,因此需要在单元格将展开(因为内容)
In this example there is a column Name which may contain very long text. The boundField will display all content in a table cell and therefore the cell will expand as needed (because of the content)
在模板列也将呈现为一个单元,但它包含了一个 DIV 其中的限制在宽度任何contet到例如40像素。因此,这列将有某种最大宽度的!
The TemplateField will also be rendered as a cell BUT it contains a div which limits the width of any contet to eg 40px. So this column will have some kind of max-width!
<asp:GridView ID="gvPersons" runat="server" AutoGenerateColumns="False" Width="100px">
<Columns>
<asp:BoundField HeaderText="ID" DataField="ID" />
<asp:BoundField HeaderText="Name (long)" DataField="Name">
<ItemStyle Width="40px"></ItemStyle>
</asp:BoundField>
<asp:TemplateField HeaderText="Name (short)">
<ItemTemplate>
<div style="width: 40px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis">
<%# Eval("Name") %>
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
下面是我的演示codeBehind
Here is my demo codeBehind
public partial class gridViewLongText : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
#region init and bind data
List<Person> list = new List<Person>();
list.Add(new Person(1, "Sam"));
list.Add(new Person(2, "Max"));
list.Add(new Person(3, "Dave"));
list.Add(new Person(4, "TabularasaVeryLongName"));
gvPersons.DataSource = list;
gvPersons.DataBind();
#endregion
}
}
public class Person
{
public int ID { get; set; }
public string Name { get; set; }
public Person(int _ID, string _Name)
{
ID = _ID;
Name = _Name;
}
}