)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Platform: WinForms |
Control: GridGroupingControl |
Published Date: May 13, 2015 |
Last Revised Date: December 24, 2019
Tags: resizing

Optimize the ResizeToFit method

The mechanism of ResiztoFit() 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 grid, 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

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment
Comments
Sergei Leschinksy
Jun 25, 2015
I tried both methods to adjust heights of rows in GridGroupingControl. The problems I encountered with first example: a) when scrolling reaches the end of the grid it fails (a check is needed that bottom_rowindex is within bounds); b) when you scroll too fast it fails with NullReference exception at ResizeToFit (I don't know why, all indices are within range); c) the last row in the table may fall outside the scroll bar reach, so it can't be displayed. The second method is somewhat better. But there are still issues which block its use: a) several last rows in most cases fall outside scrollbar reach (when I tested it with 10000 records, I missed 6 rows); b) when I scroll the grid with mouse wheel there are rendering glitches (rows are displayed with insufficient height at first), strange but other scrolling methods (keyboard, dragging scrollbar) don't suffer from it. I wonder, does anybody succeeded to implement either one of these approaches without glitches, including the esteemed author of these recipes? Reply
QLT User
Mar 30, 2016
i am not sure this sample works. the header column does not resize to fit Reply
Amresh Soundhra Pandiyan [Syncfusion]
Mar 31, 2016
Hi QLT User, For re-sizing the column headers please us the following code for applying ReSizeToFit() method in the TableControlVScrollPixelPosChanged event method. CodeExampe: this.gridGroupingControl1.TableModel.ColWidths.ResizeToFit(GridRangeInfo.Cell(e.TableControl.CurrentCell.RowIndex, e.TableControl.CurrentCell.ColIndex), GridResizeToFitOptions.IncludeHeaders);
QLT User
Oct 24, 2016
if I decide not to go with the 1st option and use this code instead.


            gridTabbedStrategies.TableModel.ColWidths.ResizeToFit(
                GridRangeInfo.Cols(0, gridTabbedStrategies.TableModel.ActiveGridView.ViewLayout.VisibleCols), GridResizeToFitOptions.IncludeHeaders);

it seems like my my whole  grid settings vanish. why is that ?



Reply
QLT User
Oct 24, 2016
if I decide not to go with the 1st option and use this code instead.


            gridTabbedStrategies.TableModel.ColWidths.ResizeToFit(
                GridRangeInfo.Cols(0, gridTabbedStrategies.TableModel.ActiveGridView.ViewLayout.VisibleCols), GridResizeToFitOptions.IncludeHeaders);

it seems like my my whole  grid settings vanish. why is that ?



Reply
Mohanraj Gunasekaran [Syncfusion]
Oct 26, 2016

Hi QLT User,

 

We have tried your code part in our sample. But we are not able to reproduce your scenario at our end. Can you please provide the customization for your sample? it will be helpful to provide the solution at the earliest.

 

Regards,

Mohanraj G.

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon