BestFitColumns and Autosize (or fill)

26 posts, 0 answers
  1. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 22 May 2009 Link to this post

    Hello,

    On my gridviews, I'd like to be able to fill the entire width of the screen (for exmaple, using Autosize = true or

    GridViewAutoSizeColumnsMode.Fill

    but also, set the BestFitColumns, so they are not all the same size.

    Is there a way to do this or am I stuck with having to do one or the other?

    Thanks

  2. Jack
    Admin
    Jack avatar
    2209 posts
    Member since:
    Sep 2012

    Posted 22 May 2009 Link to this post

    Hi Chris Browning,

    You can call BestFit method of GridViewDataColumn regardless whether AutoSizeColumnsMode is set to Fit or not. Here is a sample:

    this.radGridView1.MasterGridViewTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill; 
    this.radGridView1.Columns[1].BestFit(); 
     

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

    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. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 22 May 2009 Link to this post

    Thanks for the reply. I see that is just for one column. I have tried calling

    gridView.MasterGridViewTemplate.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill

    gridView.MasterGridViewTemplate.BestFitColumns()
    but that doesn't work for me.


    And you can't just loop over the columns calling BestFit as BestFit is a method of a particular column type (such as GridViewTextboxColumn) but not a member of GridViewColumn.

  4. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 22 May 2009 Link to this post

    Hi again,

    I've also tried the following, with no luck... columns come out as if AutoFill is on, but Best Fit is not applied

    gridView.MasterGridViewTemplate.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill

     

    Dim name As String

     

     

    For i As Integer = 0 To gridView.Columns.Count - 1

     

    name = gridView.Columns(i).UniqueName

    gridView.Columns(name).BestFit()

     

    Next

     


    or

    gridView.MasterGridViewTemplate.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill

     

     

    For i As Integer = 0 To gridView.Columns.Count - 1

     

    gridView.Columns(name).BestFit(i)

     

    Next

     

  5. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 22 May 2009 Link to this post

    Does it matter where this is called?
    I am using a grid that is inherited from the RadGrid and calling on the inherited control on DataBindingComplete
    Thanks

  6. Jack
    Admin
    Jack avatar
    2209 posts
    Member since:
    Sep 2012

    Posted 22 May 2009 Link to this post

    Hello Chris Browning,

    Actually, BestFitColumns method does the same. It iterates over all cells and calls the BestFit method of GridViewDataColumn. It will be better to call these methods after setting the data source and after the first layout of the control. A good place would be the OnLoad method of your form. If you continue to experience this issue, please open a support ticket and send me your application. I will try to locate the issue and find a proper solution.

    I am looking forward to your reply.

    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. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 26 May 2009 Link to this post

    There were a couple of issues:
    1: I was calling this on DataBindingComplete, but as the dataset was given at design time, this was happening before the grid was populated.
    2: I can now get it to BestFit correctly, but not using fill as well.

    I'd like to be able to do the following:

    1: Set the AutoFilll to none, set BestFit, but if there is any spare space at the end, then fill the space with the last visible column. On a windows datagrid view I think you can set the AutoSize mode on a per column basis, but this doesn't seem to be possible on the Terlerik ones. Do you know of a workaround please?

    In other words, the columns should always best fit, include a scroll bar if required and at the minimum, fill the available screen.

    Thanks

  8. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 26 May 2009 Link to this post

    Jack,

    I've also noticed from your own documentation that these are exclusive

    radGridView1.MasterGridViewTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
    //...or
    radGridView1.MasterGridViewTemplate.BestFitColumns();
    from http://www.telerik.com/help/winforms/grid_columns.html

    so, previous to your other answer, I presume there is no way to mix the two.
    thanks

  9. Jack
    Admin
    Jack avatar
    2209 posts
    Member since:
    Sep 2012

    Posted 27 May 2009 Link to this post

    Hello Chris Browning,

    Yes, your assumption is correct. Currently we don't support this sizing mode. However, we plan to extend the sizing modes that RadGridView supports in near future. We will consider this scenario when developing our next versions.

    If you have any other questions, please don't hesitate to ask.

    Kind 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.

  10. Ravi Prakash
    Ravi Prakash avatar
    21 posts
    Member since:
    Jan 2010

    Posted 04 Feb 2010 Link to this post

    Hi All,
              I got the best fit method working.. I done as below

                    set - grid.MasterGridViewTemplate.AutoGenerateColumns property to true (and)
                    set - grid.MasterGridViewTemplate.AutoSizeColumnsMode  property to None (and)
                    give - this.grid.MasterGridViewTemplate.BestFitColumns(); in default constructror of that form.
                     Here -
                               Even autoscroll is set to true and if AutoSizeColumnsMode to fill then scroll bar is not showing.
                               If AutoGenerateColumns is set to false, then bestfitcolumns() is not working.
                    
              I am using Q3 2009 trail version.

    Thank You
    Ravi

            

  11. Jack
    Admin
    Jack avatar
    2209 posts
    Member since:
    Sep 2012

    Posted 04 Feb 2010 Link to this post

    Hello Ravi Prakash,

    Thant's strange. We have never observed this issue before. Please send us your application. We will try to pinpoint the issue and address it for the upcoming release. I am looking forward to your reply.

    Greetings,
    Jack
    the Telerik team


    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
    Follow the status of features or bugs in PITS and vote for them to affect their priority.

  12. Adam
    Adam avatar
    89 posts
    Member since:
    Mar 2008

    Posted 01 Feb 2012 Link to this post

    >> Yes, your assumption is correct. Currently we don't support this sizing mode. However, we plan to extend the sizing modes that RadGridView supports in near future. We will consider this scenario when developing our next versions. 

    Is this scenario something that is covered in Q3 2011?  We also would like the ability for a grid to BestFit all columns but at the very least fill the available grid space.

    Thanks,
    Adam

  13. Jack
    Admin
    Jack avatar
    2209 posts
    Member since:
    Sep 2012

    Posted 06 Feb 2012 Link to this post

    Hi Adam,

    You can customize RadGridView layout by replacing the default row layout. The following sample demonstrates how to resize the last column to fit within the available space.

    public class CustomRowLayout: TableViewRowLayout
    {
        SizeF desiredSize;
     
        public override SizeF DesiredSize
        {
            get
            {
                return desiredSize;
            }
        }
     
        public override SizeF MeasureRow(SizeF availableSize)
        {
            SizeF desiredSize = base.MeasureRow(availableSize);
     
            if (!float.IsPositiveInfinity(availableSize.Width) && desiredSize.Width < availableSize.Width)
            {
                desiredSize.Width = availableSize.Width;
            }
     
            this.desiredSize = desiredSize;
            return desiredSize;
        }
     
        public override RectangleF ArrangeCell(RectangleF clientRect, GridCellElement cell)
        {
            RectangleF cellBounds = base.ArrangeCell(clientRect, cell);
            if (cell != null && cell.ColumnInfo != null && cell.ColumnInfo.Index == ScrollableColumns.Count - 1)
            {
                if (cellBounds.Right < DesiredSize.Width)
                {
                    cellBounds.Width += DesiredSize.Width - cellBounds.Right;
                }
            }
            return cellBounds;
        }
    }

    You should also create the corresponding view definition:
    public class CustomViewDefinition : TableViewDefinition
    {
        public override IRowView CreateViewUIElement(GridViewInfo viewInfo)
        {
            GridTableElement tableElement = new GridTableElement();
            tableElement.ViewElement.RowLayout = new CustomRowLayout();
            return tableElement;
        }
    }

    Finally, use the following code to attach the view definition and handle the SizeChanged event of RadGridView:
    this.radGridView1.SizeChanged += new EventHandler(radGridView1_SizeChanged);
    this.radGridView1.ViewDefinition = new CustomViewDefinition();
    this.radGridView1.BestFitColumns();
     
    void radGridView1_SizeChanged(object sender, EventArgs e)
    {
        this.radGridView1.TableElement.ViewElement.RowLayout.InvalidateLayout();
        this.radGridView1.TableElement.ViewElement.UpdateRows(true);
    }

    If you need further assistance, do not hesitate to write back.
     
    Regards,
    Jack
    the Telerik team

    SP1 of Q3’11 of RadControls for WinForms is available for download (see what's new).

  14. Mahtab
    Mahtab avatar
    3 posts
    Member since:
    Feb 2012

    Posted 14 Feb 2012 Link to this post

    does radgrid have the same function, I mean AutoSizeColumnsMode.

  15. Adam
    Adam avatar
    89 posts
    Member since:
    Mar 2008

    Posted 15 Feb 2012 Link to this post

    Mahtab,

    Radgrid has an AutoSizeColumnsMode where it can fill available space.  radGridView1.MasterTemplate.AutoSizeColumnsMode = Fill;  However, this will have the grid always locked to the size of the Grid control, meaning if you add more columns without increasing the grid size itself, the columns will scrunch together and could potentially look bad.  When the mode is set to Fill, you will never see a horizontal scrollbar.

    The other method is to call the BestFitColumns() method and this will resize each column based on the content in that column.  This method does work as intended, but will leave empty space in your grid if your columns end up being too small.  

    The approach presented earlier in this thread shows a way to use BestFitColumns() while leaving AutoSizeColumnsMode off and having the last column take up all of the remaining grid space so your grid is always filled, but  columns can still be resized to show a horizontal scroll bar if the user needs to see bigger columns.

    -Adam

  16. Jack
    Admin
    Jack avatar
    2209 posts
    Member since:
    Sep 2012

    Posted 20 Feb 2012 Link to this post

    @Adam, thank you for the detailed description.

    Mahtab, if you still experience issues when using RadGridView and the AutoSizeColumnsMode property, please contact us and describe the exact behavior that you want to achieve. We will be glad to help. 
     
    Greetings,
    Jack
    the Telerik team

    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>

  17. Uday
    Uday avatar
    6 posts
    Member since:
    Jun 2012

    Posted 20 Apr 2012 Link to this post

    Hi telerik,

    I need to show 60 columns in gridview without horizontal scroll bar, i need to set the width of each column in such a way that finally gridview should fill with columns without white space. for this im placing gridview in a windows tablelayout panel thn im dividing tablelayoutpanel width by 60 columns im setting the obtained value to each column as width like this,

    int width = tablelayoutpanel.width / 60;
     for (int i= 1; i< this.radgridview.Columns.Count; i++)
                    {
                                            this.radgridview.Columns[i].Width = width;
                    }

    but this wont for me, the columns not getting adjusted to entire screen width, please help me with this,

    and also im setting both tablelayout panel and gridview dock to fill.

  18. Jack
    Admin
    Jack avatar
    2209 posts
    Member since:
    Sep 2012

    Posted 21 Apr 2012 Link to this post

    Hello Uday,

    I am not quite sure that I understand what you want to achieve. Could you please send me your test application and a picture or details which describe the desired result? I will be glad to help further.

    I am looking forward to your reply.
     
    Greetings,
    Jack
    the Telerik team

    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>

  19. David Parvin
    David Parvin avatar
    4 posts
    Member since:
    May 2010

    Posted 19 Dec 2012 Link to this post

    I am using the code you made in this thread for resizing my last column but it seems to be getting the desired size wrong or something.  I do not seem to get the filter type icon on the filter row for the last column when I stretch the last column to fill the grid.  I was having other issues that putting the event in fixed, but just subtracting an arbitrary amount from what it is adjusting the column by does not seem right.  I know the filter part is working because when I drag the last column to some other position the filter icon shows and works.  It almost looks like the edge of the column is under the right edge of the grid by some amount that hides that value.

    The other side of this is that the scrollbar on the bottom does not show even when these look like they are outside of the area.  When I resize my form to a size that is small enough to show the scrollbar, I still can't scroll far enough right to show that area of the column.

    I changed the code slightly to change the desired size's width by 20 less and got the filter icon showing where it belongs most of the time. 

    public override RectangleF ArrangeCell(RectangleF clientRect, GridCellElement cell)
    {
        RectangleF cellBounds = base.ArrangeCell(clientRect, cell);
        if (cell != null && cell.ColumnInfo != null && cell.ColumnInfo.Index == ScrollableColumns.Count - 1)
        {
            if (cellBounds.Right < (DesiredSize.Width - 20))
            {
                cellBounds.Width += (DesiredSize.Width - 20) - cellBounds.Right;
            }
        }
        return cellBounds;
    }

    Next when I have a grid that starts not needing a vertical scrollbar, it leaves room for it on the filter row, but not on the rest.  As soon as I resize the screen at all, the filter row is resizing correctly.  To fix this I had to do the code that is in the SizeChanged event after I attached the data to the grid.

    I am currently using the Telerik.WinControls.GridView.dll from version 2012.3.1017.40.

  20. Jack
    Admin
    Jack avatar
    2209 posts
    Member since:
    Sep 2012

    Posted 21 Dec 2012 Link to this post

    Hello David ,

    Thank you for sharing your improvement with the community.

    Your assumption is correct, the cell size is incorrectly calculated. It does not take into account the width of the row header column. The following code fixes the issue without using the "magic" constant:

    public override RectangleF ArrangeCell(RectangleF clientRect, GridCellElement cell)
    {
        RectangleF cellBounds = base.ArrangeCell(clientRect, cell);
        if (cell != null && cell.ColumnInfo != null && cell.ColumnInfo.Index == ScrollableColumns.Count - 1)
        {
            float desiredWidth = DesiredSize.Width;
            desiredWidth -= RenderColumns[0].Width + Owner.CellSpacing;
            if (cellBounds.Right < desiredWidth)
            {
                cellBounds.Width += desiredWidth - cellBounds.Right;
            }
        }
        return cellBounds;
    }

    If you have further questions do not hesitate to contact us.
     
    All the best,
    Jack
    the Telerik team
    Q3’12 of RadControls for WinForms is available for download (see what's new). Get it today.

  21. Kavita
    Kavita avatar
    1 posts
    Member since:
    Sep 2011

    Posted 04 Feb 2013 Link to this post

    Hi,

         I am using telerik grid view in wpf application. I am facing an issue in column auto resizing.My requirement is to expand column width automatically accordingly to the screen size i.e when i maximize the window , all columns use all the space of  the grid and when i reduce the size of the screen then accordingly the width should be reduced and horizontal scroll bar should appear to see all its column.


    Right now i have set one of its column width to * , If i do so then horizontal scroll bar is not set according to the width of the columns it is fixed and column gets disappear.If i set the column width * in rad grid view property then also same problem i am facing.

    If i fix all its column width then when i maximize the window all available space is consumed by an another empty column, How can i avoid this extra space to be added in the end.Is there any property to set mode of column size automatically.

    Looking forward for the reply as its very urgent to me .. Please cc the reply on kavita.aggrawal@decurtis.com also.

    Thanks

  22. Yordanka
    Admin
    Yordanka avatar
    634 posts
    Member since:
    Sep 2012

    Posted 05 Feb 2013 Link to this post

    Hello Kavita,

    Please check the following help topic. In case it doesn't help please open a separate support ticket in the corresponding WPF forum. 
     
    Greetings,
    Yordanka
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  23. Toni
    Toni avatar
    2 posts
    Member since:
    Apr 2014

    Posted 14 Apr Link to this post

    Hi,

    Is this issue solved in latest releases? How can we fill the remaining part of a WinForms RadGridView with a specific column?

    Regards

  24. Desislava
    Admin
    Desislava avatar
    574 posts
    Member since:
    Jul 2013

    Posted 17 Apr Link to this post

    Hello Toni,

    Thank you for writing.

    RadGridView supports two ways to auto size the columns:

    AutoSizeColumnsMode: columns can automatically fill the entire width of the grid. Just set the AutoSizeColumnsMode property of the desired template to GridViewAutoSizeColumnsMode.Fill.

    Best fit: the column widths can be set to fit its content by using the GridViewTemplate.BestFitColumns or GridViewDataColumn.BestFit methods. This mode distributes algorithm that attempts to fit in the header text and column data for all visible rows.

    Mixing the two ways is not possible. However, you can best fit the columns and manually adjust the last column's width according to the remaining space. You can subscribe to the RadGridView.SizeChanged event and recalculate the last column's width.

    Should you have further questions, I would be glad to help.
     
    Regards,
    Desislava
    Telerik

     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     

  25. Rio
    Rio avatar
    1 posts
    Member since:
    Jun 2014

    Posted 26 Jun Link to this post

    bookingListGrv.OptionsView.ColumnAutoWidth = false;
                bookingListGrv.ScrollStyle = ScrollStyleFlags.LiveHorzScroll;
                bookingListGrv.HorzScrollVisibility = ScrollVisibility.Always;
                bookingListGrv.BestFitColumns();
                DataTable dt = new DataTable();

                for (int i = 0; i < bookingListGrv.Columns.Count; i++)
                {
                    string columnName = bookingListGrv.Columns[i].ToString();
                    dt.Columns.Add(columnName);
                    dt.Rows.Add();
                }

             
                bookingListGrc.DataSource = dt;

  26. Desislava
    Admin
    Desislava avatar
    574 posts
    Member since:
    Jul 2013

    Posted 01 Jul Link to this post

    Hello Rio,

    Thank you for writing.

    Could you please specify what is the exact question regarding the provided code snippet? If you encounter a behavior which is not expected it would be greatly appreciated if you provide a complete code snippet as RadGridView in the Telerik UI for Winforms does not have properties like OptionsView, HorzScrollVisibility, etc.

    In addition, you should call the BestFitColumns method after the RadGridView is populated with data. Thus, the cells would be populated with data and the best fitting mechanism can calculate the width according the available content.

    I am looking forward to your reply.

    Regards,
    Desislava
    Telerik

     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     

Back to Top