gym*_*ode 12 c# asp.net gridview
我有一个GridView,它使用BoundField作为列.我正在尝试为我的UserInfo列设置最大宽度.
我尝试了很多方法,但不是很有效.下面是我的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>
Run Code Online (Sandbox Code Playgroud)
寻找有关如何设置特定列宽度的建议,这是我的UserInfo专栏.
Pil*_*anz 22
我为你做了一个小演示.演示如何显示长文本.
在此示例中,列名称可能包含非常长的文本.该绑定列将显示在一个表中的所有单元格的内容,因此,根据需要细胞将扩大(因为内容的)
该模板列也将被呈现为一个单元,但它含有的div这限制了宽度任何contet到例如40像素的.所以这个专栏将有一些最大宽度!
<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>
Run Code Online (Sandbox Code Playgroud)

这是我的演示代码后面
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;
}
}
Run Code Online (Sandbox Code Playgroud)
在绑定字段中添加HeaderStyle:
<asp:BoundField HeaderText="UserId"
DataField="UserId"
SortExpression="UserId">
<HeaderStyle Width="200px" />
</asp:BoundField>
Run Code Online (Sandbox Code Playgroud)