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. Image for the cookie policy date
close icon

Sizing GridControl column to data size

I have a large dataset which is rapidly updating.
Each update triggers the QueryCellInfo in order to populate the cell.
I wish to size the column to the size of the data.
Calling ResizeColumnsToFit() after each update seems to performs very slowly.  I believe this is because this method is addressing the whole table, rather than just the cell (and thus column) which has updated.
So I tried manually sizing the column within the QueryCellInfo.  But this causes an exception
eg
private void OnGridQueryCellInfo(object sender, GridQueryCellInfoEventArgs e)
        {
            ...
            var size = e.Style.CellModel.CalculatePreferredCellSize(e.Cell.RowIndex, e.Cell.ColumnIndex, e.Style, GridQueryBounds.Width);
 
            if (size.Width > grid.ColumnWidths[e.Cell.ColumnIndex])
            {
                grid.ColumnWidths[e.Cell.ColumnIndex] = size.Width;
            }
        }
Is there an event subsequent to the QueryCellInfo I can attach to which wouldn't cause this exception?
Or an alternate solution?



3 Replies

KB Kanimozhi Bharathi Syncfusion Team November 3, 2014 05:45 PM UTC

Hi Craig,

 

Thank you for using syncfusion products.

 

We have analyzed your query. You can achieve your requirement by using ResizeColumnsToFit() like the below code snippet.

 

Code Snippet:

 

private void Button_Click(object sender, RoutedEventArgs e)

{

    var row = this.grid.CurrentCell.RowIndex;

    var col = this.grid.CurrentCell.ColumnIndex;

    this.grid.Model.ResizeColumnsToFit(GridRangeInfo.Cell(row,col),GridResizeToFitOptions.None);

    this.grid.InvalidateVisual(true);

}

 

We have also prepared a simple sample based on your requirement. Please find the attachment below

 

Please let us know if you have any queries.

 

Thanks

Kanimozhi B


Attachment: Resizetofit_demo_31496542.zip


CD Craig Daniel November 4, 2014 09:52 AM UTC

As you can see from my original query I am already aware of ResizeColumnsToFit(), however in a situation where I have lots of cell updates this method performs poorly.


KB Kanimozhi Bharathi Syncfusion Team November 10, 2014 07:23 PM UTC

Hi Craig,

Apologize for the delay.

We have analyzed your query. But Currently GridControl do not have support to adjust the column width in QueryCellInfo event or any other subsequent events. However you can resize the particular cell by calling ResizeColumnsToFit method by passing argument as particular range instead of passing whole column or table like the below snippet as we have already explained in previous update.

this.grid.Model.ResizeColumnsToFit(GridRangeInfo.Cell(row,col),GridResizeToFitOptions.None);

Please let us know if you have any queries.

Thanks

Kanimozhi B


Loader.
Live Chat Icon For mobile
Up arrow icon