Dynamically changing the gridview column width function share

  • 2020-06-01 10:51:54
  • OfStack

I usually use GridView to bind datatable, and since I need to dynamically bind to different datatable, I need to dynamically adjust the width of GridView. So I wrote this function to do that. The width of GridView needs to be accumulated according to the maximum width in each column. The maximum character width of each column needs to be distinguished between Chinese and English, because the "sequence number" and "id" of the string have an length property of 2, but the width occupied by 1 character is equivalent to 2 English characters. To achieve accurate display effect, I according to the number of characters to a string containing the target characters that specifies the length of the string equivalent English characters, for example, the string "serial number id length properties of 4, myself through function to obtain the length is 6. Each column was determined after the maximum number of characters, the width of the accumulator can obtain GridView characters, and then take in a character display on the screen width is GridView width after oneLetterLength constants.


public  void SetGridViewWidth(GridView gridview1)
        {
            int rowcount = gridview1.Rows.Count;   // The number of rows 
            int colcount = gridview1.Columns.Count;  // The number of columns 
            int i=0,j=0;
            int[] cellwidth = new int[colcount];   // An array is used to store the maximum number of characters for each column 
            int gridviewwidth = 0;  //GridView The width of the 
            Unit width = 0;  
            string temp = null;
            int tempLength = 0;
            for (i = 0; i < rowcount; i++)     // Loop through the data items to get the maximum character widths for each column 
            {
                for (j = 0; j < colcount; j++)
                {
                    temp = gridview1.Rows[i].Cells[j].Text;
                    tempLength = LengthOfLetter(temp);      //LengthOfLetter() Returns the width of the Chinese character string, 1 The Chinese characters 2 Characters wide 
                    if (cellwidth[j] < tempLength)
                    {
                        cellwidth[j] = tempLength;        // Store a large width value 
                    }
                }
            }
           
            for (j = 0; j < colcount; j++)
            {
                if (gridview1.HeaderRow.Visible == true)      // if GridView The header is visible, and the header column width is compared                 {
                    temp = gridview1.HeaderRow.Cells[j].Text;
                    tempLength = LengthOfLetter(temp);
                    if (cellwidth[j] < tempLength)
                    {
                        cellwidth[j] = tempLength;
                    }
                }
                if (gridview1.FooterRow.Visible == true)    // if GridView The tail of the table is visible, and the tail column width 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)     // Adds the maximum character widths of the columns displayed                 {
                    gridviewwidth += cellwidth[j];
                }
            }
            width = gridviewwidth * oneLetterLength;  //GridView The maximum number of characters multiplied by 1 The character display width is obtained GridView According to the width of the 
            if (gridview1.Width.Value < width.Value)    // If it's already set on the screen GridView The width will be dynamically computed with the width on the page 
               {                                                              // Initialize the width comparison, and if the initialization width is small, set the width to the newly adjusted width. 
                     gridview1.Width = width;
                }
        }
 
       // A string containing Chinese characters is equivalent to the length of an English string 
        public  int LengthOfLetter(string temp)  
        {
            int length = temp.Length;
            int newlength = temp.Length;
            for (int i = 0; i < length; i++)         // Iterate over each character in the string 
            {
                if (IsChineseLetter(temp, i))     //IsChineseLetter() To determine whether it is a Chinese character, add the width 1
                {
                    newlength++;
                }
            }
            return newlength;
        }

   // Determine if it is a Chinese character 
     public  bool  IsChineseLetter(string input,int index)
        {
            int code = 0;
            int chfrom = Convert.ToInt32("4e00", 16);    // Range ( 0x4e00 ~ 0x9fff ) into int ( chfrom ~ chend ) 
            int chend = Convert.ToInt32("9fff", 16);
            if (input != "")
            {
                code = Char.ConvertToUtf32(input, index);    // Get the string input Specified index in index In character unicode coding 

               if (code >= chfrom && code <= chend)     
               {
                    return true;     // when code Return in Chinese true
                }
               else
               {
                     return false ;    // when code Return not in Chinese false
               }
            }
            return false;
        }


Related articles: