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

AutoSizeColumnsMode = Fill and fixed width columns

7 Answers 1954 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Michael Yereniuk
Top achievements
Rank 1
Michael Yereniuk asked on 02 Jun 2008, 03:40 PM
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?

7 Answers, 1 is accepted

Sort by
0
Kiril
Telerik team
answered on 03 Jun 2008, 02:15 PM
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
0
Luca
Top achievements
Rank 1
answered on 15 Nov 2017, 02:37 PM

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?

0
Hristo
Telerik team
answered on 15 Nov 2017, 03:25 PM
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.
0
Luca
Top achievements
Rank 1
answered on 15 Nov 2017, 03:40 PM

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

0
Hristo
Telerik team
answered on 16 Nov 2017, 01:40 PM
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.
0
Luca
Top achievements
Rank 1
answered on 16 Nov 2017, 02:59 PM

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

0
Hristo
Telerik team
answered on 20 Nov 2017, 02:29 PM
Hi Luca,

Thank you for writing.

It looks like you are having a rather complex setup, the approach you are using is correct. The key is to calculate the remaining space and use it as a width to the column you need to, otherwise, it would be set for the last column in your template.

I hope this helps. Let me know if you need further assistance.

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.
Tags
GridView
Asked by
Michael Yereniuk
Top achievements
Rank 1
Answers by
Kiril
Telerik team
Luca
Top achievements
Rank 1
Hristo
Telerik team
Share this question
or