Hi Vasanth,
Thank you for usingSyncfusion Products.
We have analyzed your query and you can achieve your requirement by customizing default GridColumnSizer.For customizing, you need to create customized GridColumnSizer by deriving it from GridColumnSizer class. In the customized GridColumnSizer, you need to override the CalculateCellWidth method.
Please refer the below code snippet to create customize GridColumnSizer and override the CalculateCellWidth method.
Code Snippet:
classGridColumnAutoSizerExt:GridColumnSizer { publicGridColumnAutoSizerExt(SfDataGridgrid) :base(grid) { } ///<summary> ///Calculate Width for Column When Column Sizer is SizeToCells ///</summary> ///<param name="column">GridColumn</param> ///<returns>column width</returns> protectedoverridedoubleCalculateCellWidth(GridColumncolumn,boolsetWidth =true) { if(column.HeaderText =="City") { varcolIndex =this.DataGrid.Columns.IndexOf(column); intscrollColumnIndex =this.DataGrid.ResolveToScrollColumnIndex(colIndex); doublecolWidth = column.Width; varsource = (columnasGridComboBoxColumn).ItemsSource; stringprevtext =string.Empty; stringlengthtext =string.Empty; varclientSize =newSize(colWidth, DataGrid.RowHeight); foreach(varrinsource) { stringstr = (string)r; if(prevtext.Length < str.Length) lengthtext = str; prevtext = str; } doublewidth = Getwidth(lengthtext, clientSize); returnwidth; } else returnbase.CalculateCellWidth(column, setWidth); } doubleGetwidth(stringDisplayText,Sizesize) { TextBlockTextBlock =newTextBlock() { FontFamily = FontFamily, Margin = Margin, FontSize = FontSize }; TextBlock.Text = DisplayText; varparentBorder =newBorder{ Child = TextBlock }; TextBlock.MaxHeight = size.Height; TextBlock.MaxWidth =double.MaxValue; parentBorder.Measure(newSize(TextBlock.MaxWidth, TextBlock.MaxHeight)); parentBorder.Child =null; returnparentBorder.DesiredSize.Width; } } |
After creating customized GridColumnSizer, you need to add the customized GridColumnSizer as shown in below code snippet,
publicsealedpartialclassMainPage:Page { publicMainPage() { this.InitializeComponent(); //add customized GridColumnsSizer to dataGrid datagrid.GridColumnSizer =newGridColumnAutoSizerExt(datagrid); } } |
We have prepared a sample for your requirement also and please refer the sample from below location.
Sample location:
http://www.syncfusion.com/downloads/support/forum/118993/WinrtGridSample-1918847053.zip
Please let us know if you have any queries.
Thanks,
Gobikrishnan M