BestFitColumns and Autosize (or fill)

11 Answers 10487 Views
GridView
Richard Slade
Top achievements
Rank 2
Richard Slade asked on 22 May 2009, 09:08 AM
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

11 Answers, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 22 May 2009, 02:33 PM
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.
Richard Slade
Top achievements
Rank 2
commented on 26 May 2009, 12:44 PM

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
Richard Slade
Top achievements
Rank 2
commented on 26 May 2009, 04:02 PM

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
0
Jack
Telerik team
answered on 27 May 2009, 09:00 AM
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.
Adam
Top achievements
Rank 2
commented on 01 Feb 2012, 07:43 PM

>> 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
0
Ravi Prakash
Top achievements
Rank 1
answered on 04 Feb 2010, 06:12 AM
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

        
Jack
Telerik team
commented on 04 Feb 2010, 09:17 AM

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.
0
Jack
Telerik team
answered on 06 Feb 2012, 10:46 AM
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).

Mahtab
Top achievements
Rank 1
commented on 15 Feb 2012, 05:39 AM

does radgrid have the same function, I mean AutoSizeColumnsMode.
0
Adam
Top achievements
Rank 2
answered on 15 Feb 2012, 12:41 PM
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
Jack
Telerik team
commented on 20 Feb 2012, 09:22 AM

@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 >>
Uday
Top achievements
Rank 1
commented on 20 Apr 2012, 07:50 AM

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.
Jack
Telerik team
commented on 21 Apr 2012, 03:02 PM

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 >>
0
David Parvin
Top achievements
Rank 1
answered on 19 Dec 2012, 10:53 PM
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.
0
Jack
Telerik team
answered on 21 Dec 2012, 12:03 PM
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.
Kavita
Top achievements
Rank 1
commented on 04 Feb 2013, 11:35 AM

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
Yordanka
Telerik team
commented on 05 Feb 2013, 11:48 AM

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.

Toni
Top achievements
Rank 1
commented on 14 Apr 2014, 06:06 PM

Hi,

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

Regards
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 17 Apr 2014, 12:08 PM
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.

 
Rio
Top achievements
Rank 1
commented on 27 Jun 2014, 01:57 AM

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;
Dess | Tech Support Engineer, Principal
Telerik team
commented on 01 Jul 2014, 10:07 AM

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.
 
Michael
Top achievements
Rank 1
commented on 20 Jun 2017, 04:02 PM

Hi,

In my RadGridView (winforms) I have 3 columns.  I would like the last column to always remain a certain width in pixels (its a checkbox column) and the other columns to fill the space as the user resizes.  How can I achieve this?  Thanks.

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 21 Jun 2017, 08:33 AM
Hello Michael, 

Thank you for writing.  

It is suitable to specify the MinWidth and MaxWidth properties to the desired value and set the RadGridView.AutoSizeColumnsMode property to Fill. Thus, when you resize the grid, the columns will have a fixed size:
public RadForm1()
{
    InitializeComponent();
 
    this.radGridView1.Columns.Add("Col1");
    this.radGridView1.Columns.Add("Col2");
    this.radGridView1.Columns.Add("Col3");
 
    this.radGridView1.Columns.Last().MaxWidth = 50;
    this.radGridView1.Columns.Last().MinWidth = 50;
    this.radGridView1.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;
}

I hope this information helps. Should you have further questions I would be glad to help.

Regards,
Dess
Progress Telerik
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Michael
Top achievements
Rank 1
commented on 21 Jun 2017, 12:41 PM

That worked! Thanks.  
Zygmunt
Top achievements
Rank 1
Iron
Iron
Iron
commented on 06 Feb 2019, 10:07 AM

Is it possible to resize last column to fit contents of the cell and the remaining columns fill the width of the grid?

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 06 Feb 2019, 12:35 PM
Hello, Zygmunt,         

You can resize a certain column to fit its content by calling the GridViewDataColumn.BestFit method. Alternatively, you can double click the separator between the column header cells in order to best-fit the column on the left side of the mouse. Thus, the respective column's size will be adjusted according to its content and if the RadGridView.AutoSizeColumnsMode property is set to Fill, the rest of the columns will fill the rest of the available space.

Additional information about columns resizing is available in the following help article: https://docs.telerik.com/devtools/winforms/controls/gridview/columns/resizing-columns-programatically

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
-1
Jack
Telerik team
answered on 22 May 2009, 11:50 AM
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.
Richard Slade
Top achievements
Rank 2
commented on 22 May 2009, 12:19 PM

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.
Richard Slade
Top achievements
Rank 2
commented on 22 May 2009, 12:33 PM

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

 

Richard Slade
Top achievements
Rank 2
commented on 22 May 2009, 12:40 PM

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
Tags
GridView
Asked by
Richard Slade
Top achievements
Rank 2
Answers by
Jack
Telerik team
Ravi Prakash
Top achievements
Rank 1
Adam
Top achievements
Rank 2
David Parvin
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or