SfDataGrid Column Auto width

I'm using SfDataGrid , but there is a problem with Column width.

I want to set width of column depends on column value length and extra space  set to all columns not to one column.

Untitled.png


3 Replies

MA Mohanram Anbukkarasu Syncfusion Team November 30, 2021 11:39 AM UTC

Hi Hossein, 
 
Thanks for using Syncfusion products.  
 
We would like to le you know that you can set width of the columns based on the cell content by setting SfDataGrid.ColumnSizer as Auto. But we are little unclear about your requirement “extra space  set to all columns not to one column.”. We suspect that you need to increase the width of all the columns in addition to the default width set by the ColumnSizer. Kindly confirm this and revert to us with more details about your requirement with illustrations, if we have misunderstood your requirement.  
 
Regards, 
Mohanram A. 



HT Hossein Tavakoli December 3, 2021 07:37 AM UTC

Thanks for your response

There is some sample of Extra space that is my mind.

Untitled.jpg

I want column space computed same as Auto , but extra space set to all column not to one column.



MA Mohanram Anbukkarasu Syncfusion Team December 3, 2021 03:02 PM UTC

Hi Hossein, 

Thanks for the update.  

You can achieve your requirement to add extra space for the columns when the ColumnSizer is set as Auto by creating custom GridColumnSizer as shown below.  

Code example :  

this.dataGrid.GridColumnSizer = new GridColumnSizerExt(dataGrid); 
 
public class GridColumnSizerExt : GridColumnSizer 
{ 
 
    public GridColumnSizerExt(SfDataGrid dataGrid) 
        : base(dataGrid) 
    { 
 
    } 
 
    protected override double CalculateAutoFitWidth(GridColumn column, bool isAuto = false) 
    { 
        var width = 100 + base.CalculateAutoFitWidth(column, isAuto); 
 
        return width; 
    } 
} 


Please let us know if  you require any other assistance from us.  

Regards, 
Mohanram A. 


Loader.
Up arrow icon