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

GridComboBoxColumn resize to options.

Hi,

I have a GridComboBoxColumn column with ColumnSizer set to Auto, it resizes based on both the header & selected text. I am wondering if there a way column width can be resized to longest option or header text whichever is longer?

For example: 
Lets say, header text is "Employee Name" and below are the options:
Option 1
Option 2
Option Last With Lengthy text

with ColumnSizer  set to Auto,
a.  when there is no option selected, it sizes to fit header("Employee Name")
b.  when an option is preselected, it sizes to whichever is longer(header or selected option text).
How can I make the column width to size to longest text either in header or drop down options. So in this case longest text would to "Option Last With Lengthy text"

Second question is related to the width of the combobox options selection window. Currently selection window resizes to the displayed options. Lets say options are: 1, 2, 3, 4, 5, 6, 7 and "A long Text", when expanding drop down by default shows 1-6 options and when you scroll to last option it increases the selection window width to show the last option full text. Is it possible to make that selection window size fixed and size to longest option? 


3 Replies

GM Gobikrishnan Murugesan Syncfusion Team May 4, 2015 05:39 PM UTC

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





VA Vasanth May 4, 2015 08:13 PM UTC

your solution works great, thanks!


SC Saranya CJ Syncfusion Team May 5, 2015 06:40 AM UTC

Hi Vasanth,

Thank you for your update. Please let us know if you require further assistance on this.

Regards,
Saranya

Loader.
Live Chat Icon For mobile
Up arrow icon