Reputation: 6552
ASP.NET DataGrid, after I bind data to the GridView, how can I adjust the column header width so it's not wrapped.
here is the code behind
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>
I have tried
<asp:BoundField DataField="ContactInfo" HeaderText="Contact Info" ItemStyle-Width = "300px" SortExpression="ContactInfo" />
<asp:BoundField DataField="ContactInfo" HeaderText="Contact Info" HeaderStyle-Width="300px" SortExpression="ContactInfo" />
Upvotes: 1
Views: 1839
Reputation: 1442
You are Adjust your header column in Row data bound Event of Grid View Using following Code
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[ColumnIndex].Width = Unit.Pixel(Width in Number);
}
Upvotes: 1
Reputation: 176906
you can also try this
private void grv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
e.Row.Cells[3].Width = Unit.Pixel(300);
}
you can also try CSS solution for not wrapping header row of
th {
white-space: nowrap;
}
Upvotes: 2