2011-11-25 98 views
11

我有一個GridView,它使用BoundField作爲列。我正在設置我的UserInfo列的最大寬度。ASP.NET在GridView中設置DataBound列的寬度

我已經嘗試了許多方法,但都沒有工作。下面是我的的GridView代碼:我如何能設置一個特定的列,這是我UserInfo列的寬度

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

尋找建議。

+0

在CSS屬性的那只是給「寬度:自動」 它會自動擴大列 – user2031620

回答

22

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

在這個例子中有一個列名可能包含很長的文本。所述的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> 

enter image description here

這裏是我的演示代碼隱藏

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

添加HeaderStyle在綁定字段:

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

       <HeaderStyle Width="200px" /> 

</asp:BoundField> 
+0

謝謝,但它似乎並沒有工作。由於輸入較長,我的列仍然垂直延伸 – gymcode

+0

不起作用 – gymcode

+0

這就是我一直在尋找的,謝謝! – mbomb007

3

寬度可以設置爲如下特定列: 按百分比:

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

OR

通過像素:

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

百分比從哪裏得到?從價值的百分比? – gymcode

+1

它不適用於兩個 – gymcode

+0

您是否需要動態設置寬度?謝謝你的時間。 –

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

它對我不起作用 – gymcode

+0

@RUUHAO:對不起..它對我來說工作正常 – Karthik

+0

它也適用於我..謝謝 –

1

這將適用於所有情況與寬度工作時。

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

哎最近我想通了如何設置寬度如果你使用SQL數據綁定的GridView設置dataset.first這些控制RowStyle-Wrap="false" HeaderStyle-Wrap="false",然後你可以儘可能多的,只要你喜歡設置列寬。例如:ItemStyle-Width="150px" HeaderStyle-Width="150px"