How to resize the column widths proportionately when the grid is resized in the virtual grid in WinForms Grid Control?
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.
//Enable auto resizing the columns when Grid is resized.
this.gridControl1.AllowProportionalColumnSizing = true;'Enable auto resizing the columns when Grid is resized.
Me.gridControl1.AllowProportionalColumnSizing = TrueStep
2: Using
workaround
To auto-size
the columns, you can use the 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 the grid.QueryColWidth and provide the size of the column widths by multiplying this ratio by the grid's width. Set e. Handled = true. You can refer to the following codes.
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;
}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 SubSamples:
C#: ColumnResizeInGC
VB: ColumnResizeInGC
Reference Link: Virtual Grid
Conclusion
I hope you enjoyed learning how to resize the column widths proportionately when the grid is resized in the virtual grid in WinForms Grid Control.
You can refer to our WinForms GridControl feature tour page to know about its other groundbreaking feature representations and WinForms GridControl documentation, and how to quickly get started for configuration specifications.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!