This is a migrated thread and some comments may be shown as answers.

Column resizing using ALL data in grid

6 Answers 166 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jon Rowland
Top achievements
Rank 1
Jon Rowland asked on 27 May 2009, 03:50 AM
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

6 Answers, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 27 May 2009, 07:59 AM
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.
0
Prad
Top achievements
Rank 2
answered on 02 Jun 2009, 05:25 PM
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.
0
Ben
Top achievements
Rank 1
answered on 02 Jun 2009, 11:18 PM
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; 
            }    
        } 
 

0
Jack
Telerik team
answered on 03 Jun 2009, 09:31 AM
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.
0
erwin
Top achievements
Rank 1
Veteran
Iron
answered on 03 Jun 2009, 02:08 PM
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
0
Prad
Top achievements
Rank 2
answered on 03 Jun 2009, 03:04 PM
Erwin,  I agree with your view.

Tags
GridView
Asked by
Jon Rowland
Top achievements
Rank 1
Answers by
Jack
Telerik team
Prad
Top achievements
Rank 2
Ben
Top achievements
Rank 1
erwin
Top achievements
Rank 1
Veteran
Iron
Share this question
or