Category / Section
How to allocate equal size for each of the columns in all the tables of the WinForms GridGroupingControl?
3 mins read
Allocate equal size for each column
The parent or child table’s column width can be set equally in proportion to the grid client’s width by dynamically setting the column’s width in the TableModel.QueryColWidth event handler. When it deals with a nested table, the QueryColWidth event of the entire nested table must be handled to set the respective nested table’s column width.
void TableModel_QueryColWidth(object sender, Syncfusion.Windows.Forms.Grid.GridRowColSizeEventArgs e)
{
//Allows editing,grouping,filtering operations.
GridTableModel gridModel = sender as GridTableModel;
GridTableControl grid = (GridTableControl)gridModel.ActiveGridView;
if (gridModel != null && grid != null)
{
if (VScrollVisible)
//Gets the information about vertical scrollbar.
VSBarSize = SystemInformation.VerticalScrollBarWidth;
indentCols = (gridModel.GetColumnIndentCount()) * grid.GroupingControl.TableOptions.IndentWidth;
//Checks whether the table is a parent table or not.
if (grid.TableDescriptor.Name == "ParentTable")
{
if (e.Index > gridModel.Cols.HeaderCount + gridModel.GetColumnIndentCount() - 1)
{
parentIndentCols = VScrollVisible ? indentCols : indentCols - SystemInformation.VerticalScrollBarWidth;
availableArea = grid.GroupingControl.ClientSize.Width - indentCols - VSBarSize;
e.Size = (int)availableArea / (grid.TableDescriptor.VisibleColumns.Count);
e.Handled = true;
}
}
//Checks whether the table is a child table or not.
if (grid.TableDescriptor.Name == "ChildTable")
{
if (e.Index > gridModel.Cols.HeaderCount + gridModel.GetColumnIndentCount() - 1)
{
childIndentCols = indentCols;
availableArea = grid.GroupingControl.ClientSize.Width - gridModel.ColWidths.GetTotal(0, gridModel.Cols.HeaderCount) - indentCols - parentIndentCols;
e.Size = (int)availableArea / (grid.TableDescriptor.VisibleColumns.Count);
e.Handled = true;
}
}
//Checks whether the table is a grandchildtable or not.
if (grid.TableDescriptor.Name == "GrandChildTable")
{
if (e.Index > gridModel.Cols.HeaderCount + gridModel.GetColumnIndentCount() - 1)
{
availableArea = grid.GroupingControl.ClientSize.Width - gridModel.ColWidths.GetTotal(0, gridModel.Cols.HeaderCount) - indentCols - parentIndentCols - childIndentCols;
e.Size = (int)availableArea / (grid.TableDescriptor.VisibleColumns.Count);
e.Handled = true;
}
}
}
}
Private Sub TableModel_QueryColWidth(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.Grid.GridRowColSizeEventArgs)
'Allows editing,grouping,filtering operations.
Dim gridModel As GridTableModel = TryCast(sender, GridTableModel)
Dim grid As GridTableControl = CType(gridModel.ActiveGridView, GridTableControl)
If gridModel IsNot Nothing AndAlso grid IsNot Nothing Then
If VScrollVisible Then
'Gets the information about vertical scrollbar.
VSBarSize = SystemInformation.VerticalScrollBarWidth
End If
indentCols = (gridModel.GetColumnIndentCount()) * grid.GroupingControl.TableOptions.IndentWidth
'Checks whether the table is a parent table or not.
If grid.TableDescriptor.Name = "ParentTable" Then
If e.Index > gridModel.Cols.HeaderCount + gridModel.GetColumnIndentCount() - 1 Then
parentIndentCols = If(VScrollVisible, indentCols, indentCols - SystemInformation.VerticalScrollBarWidth)
availableArea = grid.GroupingControl.ClientSize.Width - indentCols - VSBarSize
e.Size = CInt(Fix(availableArea)) / (grid.TableDescriptor.VisibleColumns.Count)
e.Handled = True
End If
End If
'Checks whether the table is a child table or not.
If grid.TableDescriptor.Name = "ChildTable" Then
If e.Index > gridModel.Cols.HeaderCount + gridModel.GetColumnIndentCount() - 1 Then
childIndentCols = indentCols
availableArea = grid.GroupingControl.ClientSize.Width - gridModel.ColWidths.GetTotal(0, gridModel.Cols.HeaderCount) - indentCols - parentIndentCols
e.Size = CInt(Fix(availableArea)) / (grid.TableDescriptor.VisibleColumns.Count)
e.Handled = True
End If
End If
'Checks whether the table is a grandchildtable or not.
If grid.TableDescriptor.Name = "GrandChildTable" Then
If e.Index > gridModel.Cols.HeaderCount + gridModel.GetColumnIndentCount() - 1 Then
availableArea = grid.GroupingControl.ClientSize.Width - gridModel.ColWidths.GetTotal(0, gridModel.Cols.HeaderCount) - indentCols - parentIndentCols - childIndentCols
e.Size = CInt(Fix(availableArea)) / (grid.TableDescriptor.VisibleColumns.Count)
e.Handled = True
End If
End If
End If
End Sub
After
applying the properties, the grid looks like the following screenshot.
Figure 1: Equal size allocated to all the columns in all the tables
Samples: