<StyleTargetType="syncfusion:GridColumn"> <SetterProperty="ColumnSizer"Value="Auto"/> Style>
<syncfusionGrid:SfDataGrid x:Name="PlaylistsTrackListUi" DataContext="{StaticResourcePlaylistViewModel}" ItemsSource="{BindingPlaylistsView.SelectedTracksView,Mode=OneWay}" AutoGenerateColumns="False" SelectionMode="Extended" SelectionUnit="Row" SelectionChanged="SelectionChanged" RightTapped="TracksRightTappedEvent"> <syncfusionGrid:SfDataGrid.Columns> <syncfusionGrid:GridTextColumnDisplayBinding="{BindingTitle}"HeaderText="Title"MinimumWidth="100"MaximumWidth="1000"/> <syncfusionGrid:GridTextColumnDisplayBinding="{BindingArtistName}"HeaderText="Artist"MinimumWidth="100"MaximumWidth="1000"/> <syncfusionGrid:GridTextColumnDisplayBinding="{BindingAlbumName}"HeaderText="Album"MinimumWidth="100"MaximumWidth="1000"/> <syncfusionGrid:GridTextColumnDisplayBinding="{BindingDurationInHoursMinutesAndSeconds}"HeaderText="Duration"MinimumWidth="50"MaximumWidth="200"/> syncfusionGrid:SfDataGrid.Columns> syncfusionGrid:SfDataGrid>
privatevoidTracksChanged(objectsender,NotifyCollectionChangedEventArgse) { PlaylistsTrackListUi.GridColumnSizer.ResetAutoCalculationforAllColumns(); PlaylistsTrackListUi.GridColumnSizer.Refresh(); }
Sample:
https://www.syncfusion.com/downloads/support/forum/123307/ze/SfDataGridDemo116429374
Thanks,
Sivakumar
public MainPage() { this.InitializeComponent(); this.sfdatagrid.ItemsSourceChanged += Sfdatagrid_ItemsSourceChanged; } private void Sfdatagrid_ItemsSourceChanged(object sender, GridItemsSourceChangedEventArgs e) { if (this.sfdatagrid.View == null) return; this.sfdatagrid.View.SourceCollectionChanged += View_SourceCollectionChanged; } private void View_SourceCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { //Note, below process will take time to calculate width of column based on content. So all this for one time after loading data //instead of calling for each new addition in collection. this.sfdatagrid.GridColumnSizer.ResetAutoCalculationforAllColumns(); this.sfdatagrid.GridColumnSizer.Refresh(); } } |
Hi Arvin,
Thank you for your update. You can achieve your requirement by wiring the View.Records.CollectionChanged event within the ItemsSourceChanged Event. You can calculate the column width based on newly added item content in live update using View.Records.CollectionChanged Event.
private void Sfdatagrid_ItemsSourceChanged(object sender, GridItemsSourceChangedEventArgse) { if (this.sfdatagrid.View == null) return; this.sfdatagrid.View.Records.CollectionChanged += Records_CollectionChanged; } private void Records_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { this.sfdatagrid.GridColumnSizer.ResetAutoCalculationforAllColumns(); this.sfdatagrid.GridColumnSizer.Refresh(); } |
Regards,
Gnanasownthari T.