From http://blog.csdn.net/qiujiahao/archive/2007/08/09/1733347.aspx
Public void setgridviewwidth (gridview gridview1)
{
Int rowcount = gridview1.rows. Count; // number of rows
Int colcount = gridview1.columns. Count; // Number of Columns
Int I = 0, j = 0;
Int [] cellwidth = new int [colcount]; // an array is used to store the maximum number of characters in each column.
Int gridviewwidth = 0; // gridview width
Unit width = 0;
String temp = NULL;
Int templength = 0;
For (I = 0; I <rowcount; I ++) // loop data item to obtain the maximum character width of each column
{
For (j = 0; j <colcount; j ++)
{
Temp = gridview1.rows [I]. cells [J]. text;
Templength = lengthofletter (temp); // lengthofletter () returns the string width containing Chinese characters, one Chinese Character and two characters in width
If (cellwidth [J] <templength)
{
Cellwidth [J] = templength; // store large width values
}
}
}
For (j = 0; j <colcount; j ++)
{
If (gridview1.headerrow. Visible = true) // If the gridview header is visible, compare the header width {
Temp = gridview1.headerrow. cells [J]. text;
Templength = lengthofletter (temp );
If (cellwidth [J] <templength)
{
Cellwidth [J] = templength;
}
If (gridview1.footerrow. Visible = true) // if the end of the gridview table is visible, the width of the end of the table is compared {
Temp = gridview1.footerrow. cells [J]. text;
Templength = lengthofletter (temp );
If (cellwidth [J] <templength)
{
Cellwidth [J] = templength;
}
}
For (j = 0; j <colcount; j ++)
{
If (gridview1.columns [J]. Visible = true)
{
// Add the maximum character width of each column to the display {
Gridviewwidth + = cellwidth [J];
}
}
Width = gridviewwidth * 5; // The maximum number of characters in the gridview multiplied by the display width of one character to get the display width of the gridview.
If (gridview1.width. value <width. Value) // If the width of the gridview has been set on the page, the calculated width and
{// Compare the initialization width. If the initialization width is small, set the width to the adjusted width.
Gridview1.width = width;
}
}
// Display length of equivalent English strings containing Chinese Characters
Public int lengthofletter (string temp)
{
Int length = temp. length;
Int newlength = temp. length;
For (INT I = 0; I <length; I ++) // traverses each character in the string
{
If (ischineseletter (temp, I) // ischineseletter () determines whether it is a Chinese character. If yes, the width is increased by 1.
{
Newlength ++;
}
}
Return newlength;
}
// Determine whether it is a Chinese character
Public bool ischineseletter (string input, int index)
{
Int code = 0;
Int chfrom = convert. toint32 ("4e00", 16); // range (0x4e00 ~ 0x9fff) to int (chfrom ~ Chend)
Int chend = convert. toint32 ("9fff", 16 );
If (input! = "")
{
Code = Char. converttoutf32 (input, index); // obtain the Unicode encoding of the character at the specified index in the string input.
If (code> = chfrom & Code <= chend)
{
Return true; // return true if the code is within the Chinese range
}
Else
{
Return false; // if the code is not within the Chinese range, false is returned.
}
}
Return false;
}