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

Solution for Horizontal Scroll Bar and Column Fill mode problem

2 Answers 413 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Phi
Top achievements
Rank 1
Phi asked on 01 Nov 2010, 12:35 PM
Problem: You want the grid to fill up entirely so you use Column Fill mode so no empty space would be showing in the right side of the columns. In doing that, you loose the horizontal scroll bar when the grid's size is smaller than the sum of the column's width. There has been suggestion of using a scrollable panel to host the grid. But that solution is not ideal.

Solution: It is very simple. Just turn off the Column Fill mode so the horizontal scroll bar would be available when needed. Then we just have to do what the Column Fill mode is doing manually

Below is the code that I came up with: Subscribe to the grid's gridView_Resize() event so it would call FillWidth() when there is a resize event. Where ever in your code when you show the grid the first time, just call to FillWidth() so it would look right in the case that that gridView_Resize() event does not fire.

public void FillWidth()
{
    if (gridView.ColumnCount == 0)
        return;
 
    var width = CalcColumnsWidth();
 
    if (width < gridView.ClientSize.Width)
    {
        // get the remain empty space in grid
        width = gridView.ClientSize.Width - width;
 
        int extraWidthPerColumn = width / gridView.ColumnCount;
        int i, limit = gridView.ColumnCount - 1; // stop before the last column
 
        for (i = 0; i < limit; i++)
        {
            if (gridView.Columns[i].MaxWidth > 0 && gridView.Columns[i].Width + extraWidthPerColumn > gridView.Columns[i].MaxWidth)
            {
                // do calculation before we lost the original width
                width -= (gridView.Columns[i].MaxWidth - gridView.Columns[i].Width);
                gridView.Columns[i].Width = gridView.Columns[i].MaxWidth;
            }
            else
            {
                gridView.Columns[i].Width += extraWidthPerColumn;
                width -= extraWidthPerColumn;
            }
        }
 
        // the last column get any remaining width
        gridView.Columns[i].Width += width;
 
        // This only add to the last column
        //gridView.Columns[gridView.ColumnCount - 1].Width += gridView.ClientSize.Width - width;
    }
}
 
private int CalcColumnsWidth()
{
    int width = 0;
 
    foreach (var column in gridView.Columns)
        width += column.Width;
 
    return width + gridView.ColumnCount + 15; //take into the vertical lines between columns and vertical scrollbar
}
 
private void gridView_Resize(object sender, EventArgs e)
{
    gridView.MasterTemplate.BestFitColumns();
    FillWidth();
}
.

2 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 01 Nov 2010, 10:16 PM
Hello Phi,

I have created a few functions to calculate the exact size of the columns, there are some problems to get the width of the column when you have ComboBoxes, DateTime pickers and so on, please take a look at this thread and please let me know if you need my help with something.


Best Regards,
Emanuel Varga
0
Accepted
Phi
Top achievements
Rank 1
answered on 02 Nov 2010, 02:23 AM
I did read up on that thread. Luckily, I have have columns with normal texts so I have not run into many problem! I think if there is a property for the total width of all the columns, it would help out a lot so we don't have to figure it out by trials and errors!!!
Tags
GridView
Asked by
Phi
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Phi
Top achievements
Rank 1
Share this question
or