Articles in this section
Category / Section

How to optimize the ResizeToFit method for large number of records in WinForms GridGroupingControl?

4 mins read

Optimize the ResizeToFit method

The mechanism of ResizetoFit() method is that it checks each value of the cells and its width and then calculates the width of the column. So, when you check for rows above 44k, it takes more time than expected. The solution for this problem is, you can apply the ResizetoFit() method only for the visible layout. That is, instead of applying the ResizetoFit() method to the whole WinForms GridGrouping Control, you can apply this method to the viewable cells. You can get the viewable cells row and column index by using the TableControlVScrollPixelPosChanged event.

Refer to the following code examples for the TableControlVScrollPixelPosChanged event.

C#

//Event triggering. 
this.gridGroupingControl1.TableControlVScrollPixelPosChanged += new GridTableControlScrollPositionChangedEventHandler(gridGroupingControl1_TableControlVScrollPixelPosChanged);
void gridGroupingControl1_TableControlVScrollPixelPosChanged(object sender, GridTableControlScrollPositionChangedEventArgs e)
{
int top_rowIndex = this.gridGroupingControl1.TableControl.TopRowIndex;
// Denotes the top visible row index of the grid.
int bottom_rowindex = this.gridGroupingControl1.TableControl.TopRowIndex +
this.gridGroupingControl1.TableControl.ViewLayout.VisibleRows;
// ViewLayout.VisibleRows gives you the number of visible rows in the ViewLayout.
this.gridGroupingControl1.TableModel.ColWidths.ResizeToFit(GridRangeInfo.Cells(top_rowIndex, 0, bottom_rowindex, this.gridGroupingControl1.TableModel.ColCount), GridResizeToFitOptions.IncludeHeaders | GridResizeToFitOptions.IncludeCellsWithinCoveredRange);
}

 

VB

'hooks GridDataBoundGrid mouse down event.
Private Me.gridDataBoundGrid1.MouseDown += AddressOf gridDataBoundGrid1_MouseDown
Private Sub gridDataBoundGrid1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
If Me.gridDataBoundGrid1.Model.Selections.Count > 0 Then
Me.gridDataBoundGrid1.Model.CutPaste.Copy()
str = Clipboard.GetText()
DoDragDrop(str, DragDropEffects.Move) 
' selected range are copied, press left && right click to paste in gridcontrol
End If
End Sub

 

Another suggestion is that you can use the QueryColWidth event instead of using ResizeToFit() method. Refer to the following code example. In this event, you can change the size of each column with the help of index value.

C#

//Event trigerring.
this.gridGroupingControl1.TableModel.QueryColWidth += new GridRowColSizeEventHandler(TableModel_QueryColWidth);
void TableModel_QueryColWidth(object sender, GridRowColSizeEventArgs e)
{
    if (e.Index > 0)
        // Add the condition which satisfy your needs.
        e.Size = 150;
}

 

VB

'Event trigerring.
Private Me.gridGroupingControl1.TableModel.QueryColWidth += New GridRowColSizeEventHandler(AddressOf TableModel_QueryColWidth)
Private Sub TableModel_QueryColWidth(ByVal sender As Object, ByVal e As GridRowColSizeEventArgs)
 If e.Index > 0 Then ' Add the condition which satisfy your needs.
    e.Size = 150
 End If
End Sub

Samples:

C#: ResizeToFit_CS

VB: ResizeToFit_VB

Conclusion

I hope you enjoyed learning about how to optimize the ResizeToFit method for large number of records in WinForms GridGroupingControl.

You can refer to our WinForms GridGroupingControl’s feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms GridGroupingControl documentation to understand how to present and manipulate data.

For current customers, you can check out our WinForms 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 WinForms GridGroupingControl and other WinForms components.

If you have any queries or require clarifications, please let us know in comments below. You can also 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
Please sign in to leave a comment
Access denied
Access denied