AutoSizeColumnsMode = Fill and fixed width columns

7 posts, 0 answers
  1. Michael Yereniuk
    Michael Yereniuk avatar
    10 posts
    Member since:
    May 2008

    Posted 02 Jun 2008 Link to this post

    On a number of my gridviews I have button columns which I want to have set to a fixed width, otherwise they resize very wide which just looks weird.. To add my button column as a fixed width column I do something like this:

            Dim commandColumn As New GridViewCommandColumn() 
            commandColumn.UniqueName = "Command" 
            commandColumn.HeaderText = "" 
            commandColumn.Width = 70 
            commandColumn.MaxWidth = 70 
            commandColumn.MinWidth = 70 
            Me.GridView1.MasterGridViewTemplate.Columns.Add(commandColumn) 

    I also have the grid set to auto resize the columns with:

            Me.GridView1.MasterGridViewTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill 

    The problem is that the columns no longer take up the full width of the grid as a result of the fixed width column(s).. When I manually resize the columns, sometimes it will "snap" to the width of the grid, but I want it to be done automatically when the grid loads..

    Any ideas?
  2. Kiril
    Admin
    Kiril avatar
    158 posts

    Posted 03 Jun 2008 Link to this post

    Hi Michael Yereniuk,

    Thank you for writing.

    The RadGridView does not currently allow to allocate the extra space to any column. It is automatically added to the last column, which in your case is the command column. In the future, we will add this functionality, possibly in the service packs of Q2 2008.

    You can achieve the functionality you're looking for by setting the widths of the other columns youself, just like you do with the command column, or by calling the BestFit function on columns of your choosing.

    I hope this helps.

    Greetings,
    Kiril
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  3. Luca
    Luca  avatar
    3 posts
    Member since:
    May 2016

    Posted 4 days and 4 hours ago in reply to Kiril Link to this post

    Hi.

    I found this old answer but i don't find the way to select the column to allocate extra-space but i don't foud this function.

    Can you help me?

  4. Hristo
    Admin
    Hristo avatar
    1059 posts

    Posted 4 days and 3 hours ago Link to this post

    Hi Luca,

    Thank you for writing.

    Setting the AutoSizeColumnsMode property to Fill will calculate equal sizes for each of the columns. They will be set to such values that the available space in the grid table element is filled completely. It is also possible to set an individual width to a particular column: 
    this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
    this.radGridView1.Columns[1].Width = 350;

    You can also check the following documentation article describing the various resizing options: https://docs.telerik.com/devtools/winforms/gridview/columns/resizing-columns-programatically.

    I hope this helps. Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  5. Luca
    Luca  avatar
    3 posts
    Member since:
    May 2016

    Posted 4 days and 3 hours ago in reply to Hristo Link to this post

    Hi .

    Thank you for your quick answer.

    Let me explain better my need.

    I have a radgridview with 10 columns that needs to fill all avaiable space, so i set 

    this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;

    In that grid i need all the columns to autosize based on content, so i set 

    nameColumn.AutoSizeMode = BestFitColumnMode.AllCells;

    except for the second column that need to fill all remaining space.

    What sould i do in order to obtain this result?

     

    Thank you again

  6. Hristo
    Admin
    Hristo avatar
    1059 posts

    Posted 3 days and 5 hours ago Link to this post

    Hello Luca,

    Thank you for writing.

    In order to accomplish your task, you will need to best fit the columns first and calculate what the remaining space would be. Then you can adjust the width of the desired column according to your local setup. Please check my code snippet below: 
    public partial class RadForm1 : Telerik.WinControls.UI.RadForm
    {
        public RadForm1()
        {
            InitializeComponent();
     
            this.radGridView1.DataSource = this.GetData();
            this.radGridView1.BestFitColumns(BestFitColumnMode.AllCells);
        }
     
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
     
            int desiredWidth = this.radGridView1.TableElement.Size.Width - this.radGridView1.TableElement.VisualRows[0].Size.Width - this.radGridView1.TableElement.VScrollBar.Size.Width;
            this.radGridView1.Columns[2].Width += desiredWidth;
            this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
        }
     
        private DataTable GetData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Id", typeof(int));
            dt.Columns.Add("Text", typeof(string));
            dt.Columns.Add("IsChecked", typeof(bool));
            dt.Columns.Add("DateTime", typeof(DateTime));
            for (int i = 0; i < 100; i++)
            {
                dt.Rows.Add(i, "Text " + i, i % 2 == 0, DateTime.Now);
            }
     
            return dt;
        }
    }

    I am also attaching a short video showing the result on my end.

    Regards,
    Hristo
    Progress Telerik
    Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  7. Luca
    Luca  avatar
    3 posts
    Member since:
    May 2016

    Posted 3 days and 3 hours ago in reply to Hristo Link to this post

    It works for the first time, but i need to dinamycally change the rows of the radgridview.

    After the first time desiredWidth is always 0.

    Here is my code:

    rgvItems.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.None;
                    rgvItems.MasterTemplate.DataSource = null;
                    rgvItems.MasterTemplate.Refresh();
                    rgvItems.MasterTemplate.Templates[0].DataSource = null;
                    rgvItems.MasterTemplate.Templates[0].Refresh();
                    if (e.Result != null)
                    {
                        DataSet result = e.Result as DataSet;
                        if (result != null)
                        {
                            rgvItems.MasterTemplate.DataSource = result.Tables[masterTableName];
                            rgvItems.MasterTemplate.Templates[0].DataSource = result.Tables[childTableName];
                            rgvItems.MasterTemplate.Refresh();
                            rgvItems.MasterTemplate.Templates[0].Refresh();
                        }                   
                    }
                    rgvItems.MasterTemplate.BestFitColumns(BestFitColumnMode.AllCells);
                    rgvItems.MasterTemplate.Templates[0].BestFitColumns(BestFitColumnMode.AllCells);
                    int desiredWidth = this.rgvItems.TableElement.Size.Width - this.rgvItems.TableElement.VisualRows[0].Size.Width - this.rgvItems.TableElement.VScrollBar.Size.Width;
                    this.rgvItems.Columns[masterActivePrincipleColumnName].Width += desiredWidth;
                    this.rgvItems.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
                    rgvItems.MasterTemplate.CollapseAll();

     

    Thank you

Back to Top