Asp.net simple code set GridView adaptive column width does not deform the implementation idea and code
- 2020-05-24 05:21:49
- OfStack
Dynamically bound GridView does not have a fixed number of columns and has too many (the blogger did this project with 150 or so fields), so setting the fixed width of GridView does not meet the requirements. Therefore, two methods are proposed to achieve the effect of GridView adaptive column width without deformation.
.aspx.cs
Method 1 is to set cell's wrap property to false, and method 2 is to use html to realize no wrap. & nbsp; It is 1 space, which can keep the distance between the grid line and the contents of the grid.
.aspx.cs
// in GridView Is set in the event that the row data is bound
protected void gvObjectList_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
{
// Keep the column unchanged
for (int i = 0; i < e.Row.Cells.Count; i++)
{
// methods 1 :
e.Row.Cells[i].Text = " " + e.Row.Cells[i].Text + " ";
e.Row.Cells[i].Wrap = false;
// methods 2 :
//e.Row.Cells[i].Text = "<nobr> " + e.Row.Cells[i].Text + " </nobr>";
}
}
}
Method 1 is to set cell's wrap property to false, and method 2 is to use html to realize no wrap. & nbsp; It is 1 space, which can keep the distance between the grid line and the contents of the grid.