Because the number of columns of the dynamically bound gridview is not fixed, and there are too many columns (the project created by the blogger has about 150 fields), setting the fixed width of the gridview cannot meet the requirements. To solve this problem, we have sorted out two methods to achieve the effect of self-adapting the column width without deformation in the gridview.
. Aspx. CS CopyCodeThe Code is as follows: // set it in the event after the row data of the gridview 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 ++)
{
// Method 1:
E. Row. cells [I]. Text = "" + E. Row. cells [I]. Text + "";
E. Row. cells [I]. Wrap = false;
// Method 2:
// E. Row. cells [I]. Text = "<nobr>" + E. Row. cells [I]. Text + "</nobr> ";
}
}
}
Method 1: Set the automatic line feed attribute of cell to false. Method 2: Do not wrap the line by HTML marking; it is a space, the grid lines and the content inside can be kept at a certain distance to keep the appearance beautiful.