Column resizing using ALL data in grid

7 posts, 0 answers
  1. Jon Rowland
    Jon Rowland avatar
    3 posts
    Member since:
    May 2009

    Posted 26 May 2009 Link to this post

    Hi,

    I have a grid which can contain more than one screen-ful of data. After population I call BestFitColumns() to auto-size the grid columns, but the logic is only applied to the visible rows. I want the columns to be resized based on ALL rows in the grid, since if the user scrolls down they see truncated cells.

    Is this possible?

    Regards
  2. Jack
    Admin
    Jack avatar
    2335 posts

    Posted 27 May 2009 Link to this post

    Hello Jon Rowland,

    Thank you for contacting us.

    Currently this feature is not available in RadGridView. We plan to extend our sizing algorithms in future and will consider implementing it in one of our upcoming releases.

    If you have more questions, don't hesitate to contact us.

    Regards,
    Jack
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Prad
    Prad avatar
    35 posts
    Member since:
    Apr 2008

    Posted 02 Jun 2009 Link to this post

    Workaround::
            private int GetDesiredColumnWidth(RadGridView rgvResult, int idx)  
            {  
                int res = 0;  
     
                try  
                {  
                    
     
                    if (rgvResult.RowCount > 0)  
                    {  
                        Font font = rgvResult.Font;  
                        Padding cellPadding = Padding.Empty;  
                        int topVisibleRowIndex = rgvResult.GridElement.VScrollBar.Value;  
                        GridCellElement cellElement = rgvResult.Rows[topVisibleRowIndex].Cells[idx].CellElement;  
                        if (cellElement != null)  
                        {  
                            font = cellElement.Font;  
                            cellPadding = cellElement.Padding;  
                        }  
     
                        Graphics g = TelerikPaintHelper.CreateMeasurementGraphics();  
     
                        for (int i = 0; i < rgvResult.Rows.Count; i++)  
                        {  
                            string cellValue = rgvResult.Rows[i].Cells[idx].Value.ToString();  
                            if (cellValue != null)  
                            {  
                                Size size = Size.Ceiling(g.MeasureString(cellValue, font, 0, StringFormat.GenericDefault));  
                                res = Math.Max(res, size.Width + cellPadding.Horizontal);  
                            }  
                        }  
     
                        Size hsize = Size.Ceiling(g.MeasureString(rgvResult.Columns[idx].HeaderText, font, 0, StringFormat.GenericDefault));  
                        res = Math.Max(res, hsize.Width + cellPadding.Horizontal);  
     
                        g.ReleaseHdc(g.GetHdc());  
                        g.Dispose();  
     
                        if (res == 0)  
                            res = 12;  
                    }  
                }  
                catch (Exception lobjException)  
                {  
                    //Logger.Log(lobjException);  
                }  
                return res;  
            } 

    This should resolve your issue.
  5. Ben
    Ben avatar
    28 posts
    Member since:
    Dec 2007

    Posted 02 Jun 2009 Link to this post

    Thanks for that Pradeep - just what I was looking for. FWIW here is the code I implemented - based closely on what you provided but with a couple of minor tweaks. BTW - I am sub-classing the RadGridView in my own UI grid control which is where this code is found.

    Hope it helps anyone who comes across this thread...

            protected int GetMaxColumnWidth(int columnIdx) 
            { 
                // extra padding around cells 
                const int IMAGE_PADDING = 6; 
                const int STRING_PADDING = 2; 
     
                try    
                { 
                    // get the font and padding to use in cell measuring 
                    Font font = Font;    
                    Padding padding = Padding.Empty; 
                    if (RowCount > 0) 
                    { 
                        int topVisibleRowIndex = GridElement.VScrollBar.Value; 
                        GridCellElement cellElement = Rows[topVisibleRowIndex].Cells[columnIdx].CellElement; 
                        if (cellElement != null
                        { 
                            font = cellElement.Font; 
                            padding = cellElement.Padding; 
                        } 
                    } 
     
                    using (Graphics graphics = TelerikPaintHelper.CreateMeasurementGraphics()) 
                    { 
                        // measure the header width 
                        Size headerSize = Size.Ceiling(graphics.MeasureString(Columns[columnIdx].HeaderText, font, 0, StringFormat.GenericDefault)); 
                        int width = headerSize.Width + padding.Horizontal; 
     
                        // measure the column widths for all rows 
                        for (int i = 0; i < Rows.Count; i++) 
                        { 
                            object cellValue = Rows[i].Cells[columnIdx].Value; 
                            if (cellValue == null
                                continue
     
                            int cellWidth; 
                            if (cellValue is Image) 
                            { 
                                cellWidth = ((Image)cellValue).Width; 
                                cellWidth += IMAGE_PADDING; 
                            } 
                            else 
                            { 
                                SizeF size = graphics.MeasureString(cellValue.ToString(), font, 0, StringFormat.GenericDefault); 
                                cellWidth = Size.Ceiling(size).Width; 
                                cellWidth += STRING_PADDING; 
                            } 
     
                            // keep track of the max column width 
                            width = Math.Max(width, cellWidth + padding.Horizontal); 
                        } 
     
                        // clean up 
                        graphics.ReleaseHdc(graphics.GetHdc()); 
                        graphics.Dispose(); 
                         
                        return width; 
                    } 
                }    
                catch (Exception e)    
                { 
                    Debug.WriteLine("Unable to calculate the best column width"); 
                    Debug.Write(e); 
                    return 0; 
                }    
            } 
     

  6. Jack
    Admin
    Jack avatar
    2335 posts

    Posted 03 Jun 2009 Link to this post

    Pradeep, thank you for sharing this solution with the community. Indeed, it solves the issue in this specific case. We will provide improved solution in one of our upcoming releases.

    Should you have any other questions, I will be glad to help.

    Greetings,
    Jack
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  7. erwin
    erwin avatar
    358 posts
    Member since:
    Dec 2006

    Posted 03 Jun 2009 Link to this post

    Keep in mind that the described workaround can be quite time-consuming once you have grids with thousands of rows. Sizing only to the visible rows is a good compromise in that case.

    More elaborate implementations could try to cache results of MeasureString for identical strings, or provide special cases for numeric values (where numbers with the same amount of digits should always have approx. the same width). Key is to minimize calls to MeasureString().

    regards
    Erwin
  8. Prad
    Prad avatar
    35 posts
    Member since:
    Apr 2008

    Posted 03 Jun 2009 Link to this post

    Erwin,  I agree with your view.

Back to Top
UI for WinForms is Visual Studio 2017 Ready