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

Filling Grid Width

3 Answers 194 Views
GridView
This is a migrated thread and some comments may be shown as answers.
ewart
Top achievements
Rank 1
ewart asked on 13 Oct 2008, 04:11 AM
Hi, I'm using GridViewAutoSizeColumnsMode.Fill which looks great however I want a couple of columns to have a fixed width that stays the same, and the rest to be sized appropriately by the grid.

I've tried fixing the two columns by setting their width manually with AllowResize false as well as the [Max]MinWidth... but the 
GridViewAutoSizeColumnsMode.Fill  overrides this and resizes the fixed columns.
 
This is most noticable when you drag one of the non-fixed columns to the group bar - I want fixed width columns to stay the same size and the remaining floating width columns to expand dynamically to fill.

If it's not possible for the Fill to account for fixed width columns (ideal solution/feature) then manually i could calculate the sizes myself if there is some way of knowing the width available in my grid -- obviously it changes depending on whether there is a vertical scroll bar present.

cheers
ewart

3 Answers, 1 is accepted

Sort by
0
Jack
Telerik team
answered on 13 Oct 2008, 04:11 PM
Hi ewart,

Thank you for this question.

You can fix the column size by using the following code:

this.radGridView1.Columns["City"].Width = 60; 
this.radGridView1.Columns["City"].MinWidth = 60; 
this.radGridView1.Columns["City"].MaxWidth = 60; 

However, you should set explicitly the width of all columns before the fixed one. This will ensure the correct column width for all columns. This should be done after setting the AutoSizeColumnsMode. The code snippet below demonstrates this behavior:

this.radGridView1.Columns["City"].Width = 60; 
this.radGridView1.Columns["City"].MinWidth = 60; 
this.radGridView1.Columns["City"].MaxWidth = 60; 
 
this.radGridView1.MasterGridViewTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill; 
 
this.radGridView1.Columns["Name"].Width = 100; 
 

You can get the actual row width by accessing the RowWidth property when processing the PropertyChanged event:

((GridTableElement)this.radGridView1.GridElement).TableBodyElement.PropertyChanged += new PropertyChangedEventHandler(TableBodyElement_PropertyChanged); 
void TableBodyElement_PropertyChanged(object sender, PropertyChangedEventArgs e) 
    if (e.PropertyName == "Bounds"
    { 
        int rowWidth = ((GridTableElement)this.radGridView1.GridElement).RowWidth; 
    } 
 

This code will work with our latest release Q2 2008 SP1. I strongly recommend you updating your version to SP1. We will appreciate your feedback on it.

I hope this helps. Please let me know, if you have other questions.
 

Sincerely yours,
Jack
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
ewart
Top achievements
Rank 1
answered on 13 Nov 2008, 09:46 PM
Thanks for the info Jack.    I may have misunderstood your advice, but I have been unable to get this to work.

I've created a new plain grid to demonstrate the issue.  In the example I have 4 fixed columns and two variable ones in this layout:
Fixed1  Fixed2   Var1   Var2  Fixed3  Fixed4

When I resize the Var2 column, the Fixed4 column changes size even though AllowResize is false.  It should not resize, rather var1 should be resized.  In my real world example this column is a command button with an image and it looks particularly bad so this is very important to me.

Regards
  Ewart.

  private void InitializeComponent() 
      { 
 
         Telerik.WinControls.UI.GridViewCheckBoxColumn gridViewCheckBoxColumn5 = new Telerik.WinControls.UI.GridViewCheckBoxColumn(); 
         Telerik.WinControls.UI.GridViewCheckBoxColumn gridViewCheckBoxColumn6 = new Telerik.WinControls.UI.GridViewCheckBoxColumn(); 
         Telerik.WinControls.UI.GridViewTextBoxColumn gridViewTextBoxColumn10 = new Telerik.WinControls.UI.GridViewTextBoxColumn(); 
         Telerik.WinControls.UI.GridViewTextBoxColumn gridViewTextBoxColumn11 = new Telerik.WinControls.UI.GridViewTextBoxColumn(); 
         Telerik.WinControls.UI.GridViewTextBoxColumn gridViewTextBoxColumn12 = new Telerik.WinControls.UI.GridViewTextBoxColumn(); 
         Telerik.WinControls.UI.GridViewCommandColumn gridViewCommandColumn2 = new Telerik.WinControls.UI.GridViewCommandColumn(); 
 
 
 
         //  
         // radGridView1 
         //  
         this.radGridView1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(194)))), ((int)(((byte)(217)))), ((int)(((byte)(247))))); 
         this.radGridView1.Cursor = System.Windows.Forms.Cursors.Default; 
         this.radGridView1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F); 
         this.radGridView1.ForeColor = System.Drawing.SystemColors.ControlText; 
         this.radGridView1.ImeMode = System.Windows.Forms.ImeMode.NoControl; 
         this.radGridView1.Location = new System.Drawing.Point(102, 3); 
         //  
         //  
         //  
         this.radGridView1.MasterGridViewTemplate.AllowAddNewRow = false
         this.radGridView1.MasterGridViewTemplate.AllowColumnChooser = false
         this.radGridView1.MasterGridViewTemplate.AllowColumnHeaderContextMenu = false
         this.radGridView1.MasterGridViewTemplate.AllowColumnReorder = false
         this.radGridView1.MasterGridViewTemplate.AllowDeleteRow = false
         this.radGridView1.MasterGridViewTemplate.AllowRowResize = false
         gridViewCheckBoxColumn5.AllowResize = false
         gridViewCheckBoxColumn5.DataType = typeof(bool); 
         gridViewCheckBoxColumn5.FieldAlias = "Filter.MustStartWith"
         gridViewCheckBoxColumn5.FieldName = "fixed1"
         gridViewCheckBoxColumn5.HeaderText = "fixed1"
         gridViewCheckBoxColumn5.UniqueName = "fixed1"
         gridViewCheckBoxColumn6.AllowResize = false
         gridViewCheckBoxColumn6.DataType = typeof(bool); 
         gridViewCheckBoxColumn6.FieldAlias = "Filter.MustEndWith"
         gridViewCheckBoxColumn6.FieldName = "fixed2"
         gridViewCheckBoxColumn6.HeaderText = "fixed2"
         gridViewCheckBoxColumn6.UniqueName = "fixed2"
         gridViewTextBoxColumn10.FieldAlias = "txtReferer"
         gridViewTextBoxColumn10.FieldName = "var1"
         gridViewTextBoxColumn10.HeaderText = "var1"
         gridViewTextBoxColumn10.UniqueName = "var1"
         gridViewTextBoxColumn11.FieldAlias = "Filter.Keyword"
         gridViewTextBoxColumn11.FieldName = "var2"
         gridViewTextBoxColumn11.HeaderText = "var2"
         gridViewTextBoxColumn11.UniqueName = "var2"
         gridViewTextBoxColumn11.Width = 100; 
         gridViewTextBoxColumn12.AllowResize = false
         gridViewTextBoxColumn12.FieldAlias = "column1"
         gridViewTextBoxColumn12.FieldName = "fixed3"
         gridViewTextBoxColumn12.HeaderText = "fixed3"
         gridViewTextBoxColumn12.ReadOnly = true
         gridViewTextBoxColumn12.UniqueName = "fixed3"
         gridViewTextBoxColumn12.Width = 80; 
         gridViewCommandColumn2.AllowResize = false
         gridViewCommandColumn2.FieldAlias = "column1"
         gridViewCommandColumn2.FieldName = "fixed4"
         gridViewCommandColumn2.HeaderText = "fixed4"
         gridViewCommandColumn2.MaxWidth = 50; 
         gridViewCommandColumn2.MinWidth = 50; 
         gridViewCommandColumn2.UniqueName = "fixed4"
         gridViewCommandColumn2.UseDefaultText = true
         this.radGridView1.MasterGridViewTemplate.Columns.Add(gridViewCheckBoxColumn5); 
         this.radGridView1.MasterGridViewTemplate.Columns.Add(gridViewCheckBoxColumn6); 
         this.radGridView1.MasterGridViewTemplate.Columns.Add(gridViewTextBoxColumn10); 
         this.radGridView1.MasterGridViewTemplate.Columns.Add(gridViewTextBoxColumn11); 
         this.radGridView1.MasterGridViewTemplate.Columns.Add(gridViewTextBoxColumn12); 
         this.radGridView1.MasterGridViewTemplate.Columns.Add(gridViewCommandColumn2); 
         this.radGridView1.MasterGridViewTemplate.ShowRowHeaderColumn = false
         this.radGridView1.Name = "radGridView1"
         this.radGridView1.RightToLeft = System.Windows.Forms.RightToLeft.No; 
         this.radGridView1.Size = new System.Drawing.Size(576, 149); 
         this.radGridView1.TabIndex = 29; 
         this.radGridView1.Text = "radGridViewPreview"
 
 
    private void SetWidth(GridViewDataColumn gColumn, int iWidth, bool bFixed) 
      { 
         gColumn.Width = iWidth; 
 
         if (bFixed) 
         { 
            gColumn.MaxWidth = iWidth; 
            gColumn.MinWidth = iWidth; 
         } 
      } 
 
 
private void myfunction() 
         SetWidth(radGridView1.Columns["fixed1"], 30, true); 
         SetWidth(radGridView1.Columns["fixed2"], 30, true); 
         SetWidth(radGridView1.Columns["fixed3"], 30, true); 
         SetWidth(radGridView1.Columns["fixed4"], 30, true); 
 
         radGridView1.MasterGridViewTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill; 
 
         SetWidth(radGridView1.Columns["var1"], 100, false); 
         SetWidth(radGridView1.Columns["var2"], 100, false); 
 



0
Jack
Telerik team
answered on 14 Nov 2008, 05:01 PM
Hello ewart,

Thank you for writing me back.

You are correct, there are some situations in which the last column can resize. I have omitted them. To work around this issue you can use a custom header row. Consider the following code snippet:

void radGridView1_CreateRow(object sender, GridViewCreateRowEventArgs e) 
    if (e.RowInfo is GridViewTableHeaderRowInfo) 
    { 
        e.RowElement = new MyHeaderRow(); 
    } 
 
public class MyHeaderRow : GridTableHeaderRowElement 
    StyleSheet style; 
 
    protected override Telerik.WinControls.StyleSheet GetRowStyle() 
    { 
        if (this.style == null
        { 
            DefaultStyleBuilder builder = ThemeResolutionService.GetStyleSheetBuilder( 
                this.GridControl, 
                typeof(GridTableHeaderRowElement).FullName, 
                string.Empty, 
                this.GridControl.ThemeName) as DefaultStyleBuilder; 
            this.style = new XmlStyleSheet(builder.Style).GetStyleSheet(); 
        } 
 
        return this.style; 
    } 
     
    protected override void OnMouseMove(MouseEventArgs e) 
    { 
        GridCellElement previousCell = null
        foreach (GridCellElement cell in this.Children) 
        { 
            if (cell.ControlBoundingRectangle.Contains(e.Location)) 
            { 
                GridViewDataColumn dataColumn = null
                if (previousCell != null && e.X < cell.ControlBoundingRectangle.Left + 5) 
                { 
                    dataColumn = previousCell.ColumnInfo as GridViewDataColumn; 
                } 
                if (e.X > cell.ControlBoundingRectangle.Right - 5) 
                { 
                    dataColumn = cell.ColumnInfo as GridViewDataColumn; 
                } 
                if (dataColumn != null && dataColumn.FieldName == "var2"
                { 
                    return
                } 
                break
            } 
            previousCell = cell; 
        } 
        base.OnMouseMove(e); 
    } 

We will take in consideration your scenario and will provide a more convenient solution in our future releases.

Should you have any further questions, do not hesitate to write me.
 

All the best,
Jack
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
GridView
Asked by
ewart
Top achievements
Rank 1
Answers by
Jack
Telerik team
ewart
Top achievements
Rank 1
Share this question
or