How to resize the column widths proportionately when the grid is resized in the virtual grid in WinForms GridControl?
Resize the column widths
Step 1: Using the built-in property
Set the AllowProportionalColumnSizing to True, to auto-resize the columns when the grid is resized.
C#
//Enable auto resizing the columns when Grid is resized. this.gridControl1.AllowProportionalColumnSizing = true;
VB
'Enable auto resizing the columns when Grid is resized. Me.gridControl1.AllowProportionalColumnSizing = True
Step 2: Using work-around
To auto-size the columns, you can use grid.ColWidths.ResieToFit and create an array of doubles. For example, in this array, colRatios, computes the ratio of each column width to the grid.ClientSize.Width.
This gives the proper proportion value for each column. Then you can subscribe to grid.QueryColWidth and provide the size of the column widths by multiplying this ratio with the grid's width. Set e.Handled = true. You can refer to the following codes.
C#
double gridwidth = this.gridControl1.ClientSize.Width; for (int i = 1; i <= this.gridControl1.Model.ColCount; i++) { colRatios[i - 1] = (double)(this.gridControl1.Model.ColWidths[i] / gridwidth); } … void gridControl1_QueryColWidth(object sender, GridRowColSizeEventArgs e) { e.Size = (int)(colRatios[e.Index ] * this.gridControl1.ClientSize.Width); e.Handled = true; }
VB
Dim gridwidth As Double = Me.gridControl1.ClientSize.Width For i As Integer = 1 To Me.gridControl1.Model.ColCount colRatios(i - 1) = CDbl(Me.gridControl1.Model.ColWidths(i) / gridwidth) Next i … Private Sub gridControl1_QueryColWidth(ByVal sender As Object, ByVal e As GridRowColSizeEventArgs) e.Size = CInt(Fix(colRatios(e.Index) * Me.gridControl1.ClientSize.Width)) e.Handled = True End Sub
Samples:
C#: ColumnResizeInGC
VB: ColumnResizeInGC
Reference link: https://help.syncfusion.com/windowsforms/grid-control/virtual-grid