2013-11-21 54 views
0

ASP.NET DataGrid,在將數據綁定到GridView之後,如何調整列標題寬度,使其不被包裝。ASP.NET DataGrid標題寬度

這裏是背後

using (SqlConnection _conn = new SqlConnection(_sqlServer)) 
     { 
      using (SqlCommand _cmd = _conn.CreateCommand()) 
      { 
       _cmd.CommandText = 
        "select * from Clients order by ID desc"; 
       using (SqlDataAdapter _da = new SqlDataAdapter(_cmd)) 
       { 
        DataSet _ds = new DataSet(); 
        _da.Fill(_ds); 
        gvClients.DataSource = _ds.Tables[0]; 
        gvClients.DataBind(); 
       } 
      } 
     } 

代碼的.aspx

<asp:GridView ID="gvClients" runat="server"> 
     <Columns> 
      <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID"/> 
      <asp:BoundField DataField="Client" HeaderText="Client" SortExpression="Client" /> 
      <asp:BoundField DataField="ClientID" HeaderText="Client ID" SortExpression="ClientID" /> 
      <asp:BoundField DataField="ContactInfo" HeaderText="Contact Info" SortExpression="ContactInfo" /> 
      <asp:BoundField DataField="AssignedTech" HeaderText="Assigned Tech" SortExpression="AssignedTech" /> 
      <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" /> 
     </Columns> 
    </asp:GridView> 

我已經試過

<asp:BoundField DataField="ContactInfo" HeaderText="Contact Info" ItemStyle-Width = "300px" SortExpression="ContactInfo" /> 
<asp:BoundField DataField="ContactInfo" HeaderText="Contact Info" HeaderStyle-Width="300px" SortExpression="ContactInfo" /> 
+0

你有任何可能重寫或與這些寬度值衝突的CSS?具體來說,任何與'th'或'td'有關的CSS? –

回答

1

你也可以試試這個

private void grv_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 

    if (e.Row.RowType == DataControlRowType.Header) 
     e.Row.Cells[3].Width = Unit.Pixel(300); 
} 

你也可以嘗試CSS的解決方案不包裝的

th { 
    white-space: nowrap; 
} 
0
<asp:GridView runat="server" HeaderStyle-Wrap="false"></asp:GridView> 
1

標題行您是調整的網格視圖 在行數據的標題欄勢必事件中使用下面的代碼

if (e.Row.RowType == DataControlRowType.Header) 
{ 
    e.Row.Cells[ColumnIndex].Width = Unit.Pixel(Width in Number); 
}