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
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Column autosize mode of AllCells is not working.

Thread ID:

Created:

Updated:

Platform:

Replies:

149420 Nov 25,2019 10:37 PM UTC Apr 7,2021 12:34 PM UTC WinForms 3
loading
Tags: SfDataGrid
Phunction
Asked On November 25, 2019 10:37 PM UTC

I am unable to get teh autosize to work with the columns if it is set to AllCells, it works fine if it is set to ColumnHeader

grid.AutoSizeColumnsMode = Syncfusion.WinForms.DataGrid.Enums.AutoSizeColumnsMode.AllCells;
...
grid.dt.Columns.Add(("Column1");
grid.dt.Columns.Add(("Column2");
grid.DataSource = grid.dt;  (dt is a DataTable)
...
DataRow dr = dt.Rows[1];
dr[1] = "short";
dr[2] = "really long text is truncated";


doing this after the above still makes no difference:
grid.AutoSizeColumnsMode = Syncfusion.WinForms.DataGrid.Enums.AutoSizeColumnsMode.AllCells;


Gowtham Gopalsamy [Syncfusion]
Replied On November 26, 2019 12:55 PM UTC

Hi Chris,  
    
Thanks for using Syncfusion controls.   
   
We have analyzed your provided code snippet and it’s working fine at our end. We have attached the tested sample for your reference. We suspect that you are adding the rows at runtime. You can achieve your requirement to fit the text in the cells at runtime by using AutoSizeController.ResetAutoSizeWidthForAllColumns method.     
   
Please find the below code snippet.  
   
DataTable employeeCollection = new DataTable();   
   
employeeCollection.Columns.Add("Column1"typeof(string));   
employeeCollection.Columns.Add("Column2"typeof(string));   
   
this.sfDataGrid.DataSource = employeeCollection;   
   
this.sfDataGrid.AutoSizeColumnsMode = AutoSizeColumnsMode.AllCells;   
   
private void button1_Click(object sender, EventArgs e)   
{   
    employeeCollection.Rows.Add("short""really long text is truncated");   
    employeeCollection.Rows.Add("short""really long text is truncated");   
    employeeCollection.Rows.Add("short""really long text is truncated");   
    employeeCollection.Rows.Add("short""really long text is truncated");   
   
    this.sfDataGrid.AutoSizeController.ResetAutoSizeWidthForAllColumns();   
    this.sfDataGrid.AutoSizeController.Refresh();   
   
}   
   
Please find the below sample link.   
    
   
Please refer the below UG link.  
   
   
Please let us know, if you require further assistance on this.   
    
Regards,   
Gowtham.   
 


Richard
Replied On April 6, 2021 06:01 PM UTC

I can not get this to work with a BindingList dataset.

BindingList gridItems = new BindingList();
sfDataGrid.DataSource = gridItems;
sfDataGrid.AutoSizeColumnsMode = AutoSizeColumnsMode.AllCells;
sfDataGrid.Columns.Add(new GridTextColumn()
{
MappingName = "Item",
HeaderText = "Item Number"
});
sfDataGrid.Columns.Add(new GridTextColumn()
{
MappingName = "ItemDescription",
HeaderText = "Item Description"
});
//My master/detail navigation is the object that is setting gridItems, not me.
//I've tried the following workarounds - and I've confirmed that they all fire when the data changes, but it's not setting the size.
//This one had the most success, but it only seems to size to the first row of data
sfDataGrid.View.SourceCollectionChanged += (s, e) =>
{
sfDataGrid.AutoSizeController.ResetAutoSizeWidthForAllColumns();
sfDataGrid.AutoSizeController.Refresh();
}
//Tried this too - no change at all.
sfDataGrid.View.Records.CollectionChanged += (s, e) => { ... }
//Tried this also - no change
gridItems.ListChanged += (s, e) => { ... }
//Tried this as well - no change
sfdgItems.RowValidating += (s, e) => { ... }

Mohanram Anbukkarasu [Syncfusion]
Replied On April 7, 2021 12:34 PM UTC

Hi Richard, 
  
Thanks for the update  
  
We have prepared a sample using BindingList to refresh the AutoSizeController in  SfDataGrid.View.Records.CollectionChanged event as shown in the following code example.  

Code example :  

this.sfDataGrid1.View.Records.CollectionChanged += Records_CollectionChanged; 
 
private void Records_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) 
{ 
    if (this.sfDataGrid1.View.Records.Count > 0) 
    { 
        sfDataGrid1.AutoSizeController.ResetAutoSizeWidthForAllColumns(); 
        sfDataGrid1.AutoSizeController.Refresh(); 
    } 
} 



Please let us know if you require further assistance from us.  

Regards, 
Mohanram A. 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

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

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