簡體   English   中英

ASP.NET在GridView中設置DataBound列的寬度

[英]ASP.NET Setting width of DataBound column in GridView

我有一個GridView,它使用BoundField作為列。 我正在嘗試為UserInfo列設置maxwidth。

我嘗試了很多方法,但不是很有效。 下面是我的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列。

我為你做了一個小演示。 演示如何顯示長文本。

在此示例中,列名稱可能包含非常長的文本。 boundField將顯示表格單元格中的所有內容,因此單元格將根據需要進行擴展 (因為內容)

TemplateField也將呈現為一個單元格,但它包含一個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;
     }
 }

在綁定字段中添加HeaderStyle:

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

在使用寬度時,這適用於所有情況。

`<asp:TemplateField HeaderText="DATE">
   <ItemTemplate>
   <asp:Label ID="Label1" runat="server" Text='<%# Bind("date") %>' Width="100px"></asp:Label>
   </ItemTemplate>
  </asp:TemplateField>`
<asp:GridView ID="GridView1" AutoGenerateEditButton="True" 
ondatabound="gv_DataBound" runat="server" DataSourceID="SqlDataSource1"
AutoGenerateColumns="False" width="600px">

<Columns>
                <asp:BoundField HeaderText="UserId" 
                DataField="UserId" 
                SortExpression="UserId" ItemStyle-Width="400px"></asp:BoundField>
   </Columns>
</asp:GridView>

嘿,我最近想出了如何設置寬度如果你的gridview數據綁定與sql dataset.first設置這些控件RowStyle-Wrap="false" HeaderStyle-Wrap="false"然后你可以設置任意多的列寬。 例如: ItemStyle-Width="150px" HeaderStyle-Width="150px"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM