ASP.NET DataGrid标题宽度

时间:2013-11-21 16:32:17

标签: c# asp.net c#-4.0 web datagrid

ASP.NET DataGrid,在将数据绑定到GridView之后,如何调整列标题宽度以使其不被包装。

这是背后的代码

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

3 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:1)

您正在网格视图的行数据绑定事件中调整标题列 使用以下代码

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

答案 2 :(得分:0)

<asp:GridView runat="server" HeaderStyle-Wrap="false"></asp:GridView>