SoFunction
Updated on 2025-03-06

Dynamically change gridview column width function sharing


public  void SetGridViewWidth(GridView gridview1)
        {
int rowcount = ;   // Number of rows
int coloncount = ;  //Number of columns
            int i=0,j=0;
int[] cellwidth = new int[colcount];   //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 the data item to obtain the maximum character width of each column
            {
                for (j = 0; j < colcount; j++)
                {
                    temp = [i].Cells[j].Text;
tempLength = LengthOfLetter(temp);        //LengthOfLetter() returns the width of the string containing Chinese characters, 1 Chinese character and 2 characters wide
                    if (cellwidth[j] < tempLength)
                    {
cellwidth[j] = tempLength;                                                                                                                        �
                    }

                }
            }

          

            for (j = 0; j < colcount; j++)
            {
if ( == true)                                                                                                                            �
                    temp = [j].Text;
                    tempLength = LengthOfLetter(temp);
                    if (cellwidth[j] < tempLength)
                    {
                        cellwidth[j] = tempLength;
                    }
                }

if ( == true)    //If the end of the GridView table is visible, the column width at the end of the table is included in the comparison                                                                                                           �
                    temp = [j].Text;
                    tempLength = LengthOfLetter(temp);
                    if (cellwidth[j] < tempLength)
                    {
                        cellwidth[j] = tempLength;
                    }
                }

            }


            for (j = 0; j < colcount; j++)
            {
if ([j].Visible == true)      //Add the maximum character widths of each column of the displayed column                                                                                                           �
                    gridviewwidth += cellwidth[j];
                }
            }

width = gridviewwidth * oneLetterLength; //Multiple the maximum number of characters in GridView by one character display width to get the GridView display width
if ( < )     //If the width of the GridView has been set on the interface, the width of the dynamic calculation will be obtained and the
{                                                                                                                                                                                                                                                              �
                     = width;
                }

        }

//The display length of the string equivalent to the English string containing Chinese

        public  int LengthOfLetter(string temp) 
        {
            int length = ;
            int newlength = ;

for (int i = 0; i < length; i++) //Transfer each character of the string
            {
if (IsChineseLetter(temp, i))     //IsChineseLetter() determines whether it is a Chinese character, then the width is 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);    //Convert range (0x4e00~0x9fff) to int (chfrom~chend)
            int chend = Convert.ToInt32("9fff", 16);
            if (input != "")
            {
code = Char.ConvertToUtf32(input, index);     //Get the character unicode encoding at the specified index in the string input
               
               if (code >= chfrom && code <= chend)    
               {
return true;

                }
               else
               {
return false ;     //Return false when the code is not in Chinese
               }
            }

            return false;
        }