C# ASP.NET设置GridView中数据绑定列的宽度

C# ASP.NET设置GridView中数据绑定列的宽度,c#,asp.net,gridview,C#,Asp.net,Gridview,我有一个GridView,它使用BoundField作为列。我正在尝试为我的UserInfo列设置最大宽度 我尝试了很多方法,但没有一种有效。下面是my GridView的代码: 正在寻找有关如何设置特定列(即我的UserInfo列)宽度的建议。在绑定字段中添加标题样式: <asp:BoundField HeaderText="UserId" DataField="UserId" SortExpression="

我有一个GridView,它使用BoundField作为列。我正在尝试为我的
UserInfo
列设置最大宽度

我尝试了很多方法,但没有一种有效。下面是my GridView的代码:



正在寻找有关如何设置特定列(即我的
UserInfo
列)宽度的建议。

在绑定字段中添加标题样式:

    <asp:BoundField HeaderText="UserId"
                DataField="UserId" 
                SortExpression="UserId">

                <HeaderStyle Width="200px" />

</asp:BoundField>

宽度可设置为特定列,如下所示: 按百分比计算:

<asp:BoundField HeaderText="UserInfo"  DataField="UserInfo"  
SortExpression="UserInfo" ItemStyle-Width="100%"></asp:BoundField>

按像素:

<asp:BoundField HeaderText="UserInfo"  DataField="UserInfo"  
SortExpression="UserInfo" ItemStyle-Width="500px"></asp:BoundField>


我为您做了一个小演示。演示如何显示长文本

在本例中,有一个列名可能包含很长的文本。边界字段将显示表格单元格中的所有内容,因此单元格将根据需要展开(因为内容)

模板字段也将呈现为一个单元格,但它包含一个div,该div将任何内容的宽度限制为40px。因此,此列将具有某种最大宽度

    <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>

这是我的演示代码

 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;
     }
 }
public分部类gridViewLongText:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
#区域初始化和绑定数据
列表=新列表();
列表。添加(新人员(1,“Sam”);
列表。添加(新人员(2,“最大”);
列表。添加(新的人(3,“Dave”);
列表。添加(新人员(4,“表格平均长名”);
gvPersons.DataSource=列表;
gvPersons.DataBind();
#端区
}
}
公共阶层人士
{
公共int ID{get;set;}
公共字符串名称{get;set;}
公众人物(整数ID、字符串名称)
{
ID=_ID;
名称=_名称;
}
}

在使用宽度时,这适用于所有情况

`<asp:TemplateField HeaderText="DATE">
   <ItemTemplate>
   <asp:Label ID="Label1" runat="server" Text='<%# Bind("date") %>' Width="100px"></asp:Label>
   </ItemTemplate>
  </asp:TemplateField>`
`
`

嘿,最近我想出了如何设置gridview与sql数据集绑定时的宽度。首先设置这些控件
RowStyle Wrap=“false”HeaderStyle Wrap=“false”
,然后您可以随意设置列宽。例如:
ItemStyle Width=“150px”HeaderStyle Width=“150px”

谢谢,但它似乎不起作用。我的专栏仍然垂直延伸,因为我的输入很长,谢谢。非常干净,简单的解决方案。百分比从哪里获得值?值的百分比?是否需要动态设置宽度?感谢您的时间。在css属性中,只需给出“width:auto”,它将自动扩展列。这对我来说也不适用。如果这很重要,我将自动生成列设置为false。
 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;
     }
 }
`<asp:TemplateField HeaderText="DATE">
   <ItemTemplate>
   <asp:Label ID="Label1" runat="server" Text='<%# Bind("date") %>' Width="100px"></asp:Label>
   </ItemTemplate>
  </asp:TemplateField>`