Articles in this section
Category / Section

How to resize the column width and row height to client size in WinForms GridControl?

2 mins read

Resize the column width and row height

By default, the row height and column width are displayed in the WinForms Grid Control in its default size. Therefore, when row count and column count is minimum than client size, the rows and columns are displayed as minimum and the remaining space is grayed. You can use the AllowProportionalColumnSizing property to set the column width as Client size. For more details, refer to this link: Resizing the Column width as Client size

Refer to the following screenshot.

Resize the row and column

Figure 1: Row and Column Resizing

Solution

You can resize the row height and column width as its client size, even when it is lesser than the client size by using two custom classes. You can use the following custom classes.

  • The GridColSizingHelper class includes the customization for column width resizing according to client size using QueryColWidth event.
  • The GridRowSizingHelper class includes the customization for row height resizing according to client size using QueryRowHeight event.

C#

#region HelperClasses Variables
private GridColSizingHelper Helper1;
private GridRowSizingHelper Helper2;
#endregion
public Form1()
{
   InitializeComponent();
   //Resizing the Column width to client size
   Helper1 = new GridColSizingHelper();
   Helper1.WireGrid(this.gridControl1);
   //Resizing the Row Height to client size
   Helper2 = new GridRowSizingHelper();
   Helper2.WireGrid(this.gridControl1);
}

VB

#Region "HelperClasses Variables"
Private Helper1 As GridColSizingHelper
Private Helper2 As GridRowSizingHelper
#End Region
Public Sub New()
    InitializeComponent()
    'Resizing the Column width to client size
    Helper1 = New GridColSizingHelper()
    Helper1.WireGrid(Me.gridControl1)
    'Resizing the Row Height to client size
    Helper2 = New GridRowSizingHelper()
    Helper2.WireGrid(Me.gridControl1)
End Sub

GridColSizingHelper class customization

C#

private void Model_QueryColWidth(object sender, GridRowColSizeEventArgs e)
{
   if(e.Index > grid.Model.Cols.HeaderCount)
   {
     //Calculating the Client size
     this.clientsize  = grid.ClientSize.Width - grid.Model.ColWidths.GetTotal(0,     this.grid.Model.Cols.HeaderCount);
     //Setting the Column width
     e.Size = (int)clientsize / grid.Model.ColCount;
     e.Handled = true;
   }
}

VB

Private Sub Model_QueryColWidth(ByVal sender As Object, ByVal e As GridRowColSizeEventArgs)
   If (e.Index > grid.Model.Cols.HeaderCount) Then     'Calculating the Client size     Me.clientsize = (grid.ClientSize.Width - grid.Model.ColWidths.GetTotal(0, Me.grid.Model.Cols.HeaderCount))     'Setting the Column width     e.Size = (CType(clientsize,Integer) / grid.Model.ColCount)     e.Handled = true
   End If
End Sub

GridRowSizingHelper class customization

C#

void Model_QueryRowHeight(object sender, GridRowColSizeEventArgs e)
{
   if (e.Index > grid.Model.Rows.HeaderCount)
   {
      //Calculating the Client size
      this.clientsize = grid.ClientSize.Height - grid.Model.RowHeights.GetTotal(0,     this.grid.Model.Rows.HeaderCount);
      //Setting the Column width
      e.Size = (int)clientsize / grid.Model.RowCount;
      e.Handled = true;
   }
}

VB

Private Sub Model_QueryRowHeight(ByVal sender As Object, ByVal e As GridRowColSizeEventArgs)   If (e.Index > grid.Model.Rows.HeaderCount) Then       'Calculating the Client size        Me.clientsize = (grid.ClientSize.Height - grid.Model.RowHeights.GetTotal(0, Me.grid.Model.Rows.HeaderCount))        'Setting the Column width         e.Size = (CType(clientsize,Integer) / grid.Model.RowCount)         e.Handled = true
   End If
End Sub

 

Note:

For GridGroupingControl, you can resize the column width based on the client size by enabling the AllowProportionalColumnSizing property.

 

C#

this.gridGroupingControl1.AllowProportionalColumnSizing = true;

VB

Me.gridGroupingControl1.AllowProportionalColumnSizing = true;

The following screenshot illustrates the resized row height and column width of Grid.

Resized row height and column width of grid

Figure 2: Resized row height and column width of Grid

Samples:

C#: Resizing_Column_and_Row_to_client_size-CS

VB: Resizing_Column_and_Row_to_client_size-VB

Conclusion


I hope you enjoyed learning about how to resize the column width and row height to client size in WinForms GridControl.

Refer to our WinForms GridControl’s feature tour page for its other groundbreaking feature representations. You can also explore our WinForms GridControl documentation to understand how to present and manipulate data.


For current customers, check out our WinForms components from the License and Downloads page. If you are new to Syncfusion, try our 30-day free trial to check out our WinForms GridControl and other WinForms components.


Please let us know in the following comment section if you have any queries or require clarifications. Contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!


Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied