Articles in this section
Category / Section

How to calculate the column width in optimized way when EnableDataVirtualization is true in UWP Grid?

2 mins read

When ColumnSizer is Auto, AutoLastColumnFill, SizeToCell and AutoWidthLastColumnFill, column widths are calculated based on all the records. In DataVirtualization case, accessing all the records will take some time for calculating the column widths. You can optimize the column width calculation based on initial display records by customizing GridColumnSizer. DataGrid.View.Records.SuspendUpdates() suspends new records creation, so column width calculated based on only available records.

C# 

           
// Assign custom GridColumnSizer to datagrid GridColumnSizer
this.sfGrid.GridColumnSizer = new ColumnSizerExt(sfGrid);
 
 
public class ColumnSizerExt : GridColumnSizer
{
    public ColumnSizerExt(SfDataGrid grid)
        : base(grid)
    {
    }
 
    //Overriding CalculateCellWidth method
    protected override double CalculateCellWidth(GridColumn column, bool setWidth = true)
    {
        if (this.DataGrid.View == null)
            return column.ActualWidth;
        return this.GetCellWidth(column);
    }
 
    private double GetCellWidth(GridColumn column)
    {
        double resultWidth = 0;
        var colIndex = this.DataGrid.Columns.IndexOf(column);
 
        int scrollColumnIndex = this.DataGrid.ResolveToScrollColumnIndex(colIndex);
        double colWidth = this.DataGrid.GetVisualContainer().ColumnWidths[scrollColumnIndex];
        double rowHeight = this.DataGrid.GetVisualContainer().RowHeights.DefaultLineSize;
        var textLength = 0;
        var prevColumnWidth = 0;
        int stringLenth = 0;
        var clientSize = new Size(colWidth, rowHeight);
        object record = null;
        
        //Need to call Records.SuspendUpdates to avoid RecordEntry creation on data virtualizaiton cases
        this.DataGrid.View.Records.SuspendUpdates();
        foreach (var recordEntry in this.DataGrid.View.Records)
        {
            if (recordEntry == null || recordEntry.Data == null)
                continue;
 
            if (column is GridTemplateColumn)
            {
                var textsize = this.GetCellSize(clientSize, column, recordEntry.Data, GridQueryBounds.Width);
                if (textsize.IsEmpty)
                    continue;
 
                if (resultWidth < textsize.Width)
                    resultWidth = textsize.Width;
            }
            else
            {
                var text = this.GetDisplayText(column, recordEntry.Data);
                if (text.Length >= stringLenth)
                {
                    stringLenth = text.Length;
                    record = recordEntry.Data;
                }
            }
        }
        this.DataGrid.View.Records.ResumeUpdates();
 
        if (!(column is GridTemplateColumn))
        {
            var textsize = this.GetCellSize(clientSize, column, record, GridQueryBounds.Width);
            resultWidth = textsize.Width;
        }
 
        textLength = 0;
        prevColumnWidth = 0;
        return Math.Round(resultWidth);
    }
 
}

 

Sample:

UWP 

Conclusion

I hope you enjoyed learning about how to calculate the column width in optimized way when EnableDataVirtualization is true.

You can refer to our UWP DataGrid feature tour page to know about its other groundbreaking feature representations. You can also explore our UWP DataGrid documentation to understand how to create and manipulate data.

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 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