Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WinForms > GridView > BestFitColumns and Autosize (or fill)

Not answered BestFitColumns and Autosize (or fill)

Feed from this thread
  • Posted on May 22, 2009 (permalink)

    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

    Reply

  • Jack Jack admin's avatar

    Posted on May 22, 2009 (permalink)

    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.

    Reply

  • Posted on May 22, 2009 (permalink)

    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.

    Reply

  • Posted on May 22, 2009 (permalink)

    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

     

    Reply

  • Posted on May 22, 2009 (permalink)

    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

    Reply

  • Jack Jack admin's avatar

    Posted on May 22, 2009 (permalink)

    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.

    Reply

  • Posted on May 26, 2009 (permalink)

    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

    Reply

  • Posted on May 26, 2009 (permalink)

    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

    Reply

  • Jack Jack admin's avatar

    Posted on May 27, 2009 (permalink)

    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.

    Reply

  • Ravi Prakash avatar

    Posted on Feb 4, 2010 (permalink)

    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

            

    Reply

  • Jack Jack admin's avatar

    Posted on Feb 4, 2010 (permalink)

    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.

    Reply

  • Posted on Feb 1, 2012 (permalink)

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

    Reply

  • Jack Jack admin's avatar

    Posted on Feb 6, 2012 (permalink)

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

    Reply

  • Mahtab avatar

    Posted on Feb 14, 2012 (permalink)

    does radgrid have the same function, I mean AutoSizeColumnsMode.

    Reply

  • Posted on Feb 15, 2012 (permalink)

    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

    Reply

  • Jack Jack admin's avatar

    Posted on Feb 20, 2012 (permalink)

    @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 >>

    Reply

  • Uday avatar

    Posted on Apr 20, 2012 (permalink)

    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.

    Reply

  • Jack Jack admin's avatar

    Posted on Apr 21, 2012 (permalink)

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

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WinForms > GridView > BestFitColumns and Autosize (or fill)
Related resources for "BestFitColumns and Autosize (or fill)"

[ Features | Demos | Documentation | Knowledge Base | Telerik TV | Code Library | Step-by-step Tutorial | Blogs | Self-Paced Trainer ]