Automatically Resizing Columns Based on Content

I have an app using an SfDataGrid which is bound to an ObservableCollection<T>, where T contains various properties bound to columns in the grid. The grid is defined as follows:

            <sf:SfDataGrid AutoGenerateColumns="True"

                           ColumnWidthMode="Auto"

                           AllowResizingColumns="True"

                           ItemsSource="{Binding FileInfo}">

            </sf:SfDataGrid>

When I populate the collection the grid updates properly...but the columns don't resize to accommodate the cell contents. I can manually adjust the widths, by clicking and dragging in the header, but I'd like to avoid having to do that.

What do I need to do to accomplish this? 




3 Replies

SS Sampathnarayanan Sankaralingam Syncfusion Team February 28, 2022 09:45 PM UTC

Hi Mark,


Based on provided details, we have created the sample and we are not able to reproduce the issue.
you have already set the
ColumnWidthMode.Auto in the provided code and it calculates the column width automatically based on the header and cell contents. Please refer the below UG and sample from below links for more details.


UG : https://help.syncfusion.com/winui/datagrid/autosize-columns


Sample Link : https://www.syncfusion.com/downloads/support/forum/173248/ze/Sample398424539


Please modify the sample to replicate the issue and also provide the video illustration. It will be more helpful for us to find the cause and provide you the prompt and timely solution.


Regards,

Sampath Narayanan.S



MA Mark February 28, 2022 10:08 PM UTC

Hi Sampath, and thanx for the quick reply.

After filing the report I did some further work and was able to get the columns to size automatically as expected. I believe the problem was on my end and resulted from embedding the SfDataGrid inside a UI element (I don't recall which one) which does not resize itself to content the way I expected. When I made the SfDataGrid a child of a StackPanel as the outermost UI element it suddenly started sizing correctly.

Sorry about the lack of details on how I resolved the issue. But is resolved.



SS Sampathnarayanan Sankaralingam Syncfusion Team March 1, 2022 11:39 AM UTC

Hi Mark,


We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this. We are happy to help you😊.


Regards,

Sampath Narayanan.S


Loader.
Up arrow icon