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

Using AutoSizeColumnsMode.Fill vs BestFitColumns

3 Answers 378 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jeremy Murtishaw
Top achievements
Rank 1
Jeremy Murtishaw asked on 17 Mar 2010, 03:30 AM
Hello,

I have a few grids where I'd like to achieve the following:

Have the column widths automatically determined via grd.MasterGridViewTemplate.BestFitColumns() except when the sum of the column widths under this method is less than the width of the grid itself. In this case, set the grd.MasterGridViewTemplate.AutoSizeColumnsMode to be Fill. 

I have tried to do this via the following code, but it doesn't seem to be accurate when summing the column widths, and thus I am still getting cases where the columns do not fill the grid...

grdDifferences.MasterGridViewTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.None; 
                grdDifferences.MasterGridViewTemplate.BestFitColumns(); 
                int iTotalWidth = grdDifferences.Width; 
                int iCountWidth = 0; 
                foreach (GridViewColumn column in grdDifferences.Columns) 
                { 
                    iCountWidth += column.Width; 
                } 
                if (iCountWidth < iTotalWidth) 
                { 
                    grdDifferences.MasterGridViewTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill; 
                } 

Any help would be appreciated.

Thanks!
Jeremy

3 Answers, 1 is accepted

Sort by
0
Accepted
Jack
Telerik team
answered on 17 Mar 2010, 06:26 PM
Hello Jeremy Murtishaw,

In this case you have to take in account the row header column and the cell spacing. I modified the code a little, please consider the following code snippet:
 

grdDifferences.MasterGridViewTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.None;
grdDifferences.MasterGridViewTemplate.BestFitColumns();
int iTotalWidth = radGridView1.Width;
int iCountWidth = 0;
foreach (GridViewColumn column in grdDifferences.Columns)
{
    iCountWidth += column.Width + ((GridTableElement)grdDifferences.GridElement).CellSpacing;
}
if (grdDifferences.MasterGridViewTemplate.ShowRowHeaderColumn)
{
    iCountWidth += ((GridTableElement)grdDifferences.GridElement).RowHeaderColumnWidth;
}
if (grdDifferences.GridElement.VScrollBar.Visibility == ElementVisibility.Visible)
{
    iTotalWidth -= grdDifferences.GridElement.VScrollBar.Size.Width;
}
if (iCountWidth < iTotalWidth)
{
    grdDifferences.MasterGridViewTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
}

I hope it helps. Please write me back, if you need further assistance.

Sincerely yours,
Jack
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Jeremy Murtishaw
Top achievements
Rank 1
answered on 18 Mar 2010, 09:51 PM
Hey Jack,

This works great! The one thing I'll add is a check to make sure the column is visible before adding its width to the count.

Thanks!
Jeremy
0
Jack
Telerik team
answered on 23 Mar 2010, 01:27 PM
Hi Jeremy, I am glad to hear that. If you need further assistance, do not hesitate to write back.

 

Best wishes,
Jack
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
GridView
Asked by
Jeremy Murtishaw
Top achievements
Rank 1
Answers by
Jack
Telerik team
Jeremy Murtishaw
Top achievements
Rank 1
Share this question
or