2011-11-25 35 views
11

Tôi có GridView sử dụng BoundField cho các cột. Tôi đang cố gắng thiết lập một maxwidth cho cột UserInfo của tôi.ASP.NET Thiết lập chiều rộng cột DataBound trong GridView

Tôi đã thử nhiều cách nhưng không có cách nào khác. Dưới đây là mã cho tôi 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 gợi ý về làm thế nào tôi có thể thiết lập độ rộng của một cột cụ thể, đó là cột UserInfo tôi.

+0

thuộc tính css mà chỉ cần cung cấp "chiều rộng: tự động" Nó sẽ tự động mở rộng cột – user2031620

Trả lời

22

Tôi đã làm một bản demo nhỏ cho bạn. Thể hiện cách hiển thị văn bản dài.

Trong ví dụ này, có một tên cột có thể chứa văn bản rất dài. Các BoundField sẽ hiển thị tất cả nội dung trong một ô trong bảng và do đó các tế bào sẽ mở rộng khi cần thiết (vì nội dung)

các TemplateField cũng sẽ được trả lại như một tế bào NHƯNG nó chứa một div trong đó giới hạn các chiều rộng của bất kỳ đường viền nào ví dụ: 40px. Vì vậy, cột này sẽ có một số loại chiều rộng tối đa!

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

enter image description here

Đây là codebehind demo của tôi

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

thêm HeaderStyle trong lĩnh vực giới hạn của bạn:

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

       <HeaderStyle Width="200px" /> 

</asp:BoundField> 
+0

Cảm ơn, nhưng dường như không hoạt động. Cột của tôi vẫn mở rộng theo chiều dọc do đầu vào dài – gymcode

+0

không hoạt động hoặc – gymcode

+0

Đây là những gì tôi đang tìm kiếm, cảm ơn! – mbomb007

3

Chiều rộng có thể được thiết lập để cột cụ thể như sau: Bằng tỷ lệ phần trăm:

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

HOẶC

By Pixel:

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

Tỷ lệ phần trăm nhận được giá trị từ đâu? phần trăm giá trị từ? – gymcode

+1

nó không hoạt động cho cả hai – gymcode

+0

Bạn có cần thiết lập chiều rộng động không? Cảm ơn vì đã dành thời gian cho tôi. –

0
<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> 
+2

nó không hoạt động cho tôi – gymcode

+0

@RUiHAO: xin lỗi ..nó hoạt động tốt cho tôi – Karthik

+0

nó làm việc cho tôi quá .. cảm ơn –

1

Điều này sẽ làm việc cho tất cả các tình huống khi làm việc với chiều rộng.

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

hey thời gian gần đây tôi figured it out làm thế nào để thiết lập chiều rộng nếu databound GridView của bạn với sql dataset.first thiết lập các kiểm soát RowStyle-Wrap="false" HeaderStyle-Wrap="false" và sau đó bạn có thể thiết lập độ rộng cột như nhiều như bạn muốn. ví dụ: ItemStyle-Width="150px" HeaderStyle-Width="150px"

Các vấn đề liên quan