ASP.NET 在 GridView 中设置 DataBound 列的宽度 [英] ASP.NET Setting width of DataBound column in GridView

查看:22
本文介绍了ASP.NET 在 GridView 中设置 DataBound 列的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

<asp:GridView ID="GridView1" AutoGenerateEditButton="True"ondatabound="gv_DataBound" runat="server" DataSourceID="SqlDataSource1"AutoGenerateColumns="假"><列><asp:BoundField HeaderText="UserId"数据字段 =用户 ID"SortExpression="UserId"></asp:BoundField><asp:BoundField HeaderText="用户名"数据字段=用户名"SortExpression="用户名"></asp:BoundField><asp:BoundField HeaderText="用户信息"数据字段="用户信息"SortExpression="UserInfo"></asp:BoundField></列></asp:GridView>

寻求有关如何设置特定列(即我的 UserInfo 列)宽度的建议.

解决方案

我为你做了一个小演示.演示如何显示长文本.

在此示例中,有一列名称可能包含很长的文本.boundField 将显示表格单元格中的所有内容,因此单元格将根据需要扩展(因为内容)

TemplateField 也将呈现为一个单元格,但它包含一个 div,它限制宽度任何内容到例如 40px.所以这个列会有某种最大宽度!

 <asp:GridView ID="gvPersons" runat="server" AutoGenerateColumns="False" Width="100px"><列><asp:BoundField HeaderText="ID" DataField="ID"/><asp:BoundField HeaderText="Name (long)" DataField="Name"><ItemStyle Width="40px"></ItemStyle></asp:BoundField><asp:TemplateField HeaderText="Name (short)"><项目模板><div style="width: 40px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis"><%#Eval("姓名")%>

</ItemTemplate></asp:TemplateField></列></asp:GridView>

这里是我的演示代码

 公共部分类 gridViewLongText : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){#region 初始化并绑定数据列表<人>list = new List();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 = 列表;gvPersons.DataBind();#endregion}}公开课人{公共 int ID { 获取;放;}公共字符串名称 { 获取;放;}公共人(int _ID,字符串 _Name){ID = _ID;姓名 = _姓名;}}

I have a GridView which uses BoundField for columns. I am trying to set a maxwidth for my UserInfo column.

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>

Looking for suggestions on how I can set the width of a specific column, which is my UserInfo column.

解决方案

I did a small demo for you. Demonstrating how to display long text.

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)

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>

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

这篇关于ASP.NET 在 GridView 中设置 DataBound 列的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆