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

Column autosize mode of AllCells is not working.

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;


3 Replies

GG Gowtham Gopalsamy Syncfusion Team 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.   
 



RI Richard 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) => { ... }


MA Mohanram Anbukkarasu Syncfusion Team 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. 


Loader.
Live Chat Icon For mobile
Up arrow icon