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
.
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();
}