Asp.net simple code sets the implementation idea and code of the gridview adaptive column width without deformation

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.