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

GridComboBoxColumn resize to options.

Thread ID:

Created:

Updated:

Platform:

Replies:

118993 May 1,2015 04:25 PM May 5,2015 02:40 AM WinRT 3
loading
Tags: SfDataGrid
Vasanth
Asked On May 1, 2015 04:25 PM

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? 


Gobikrishnan Murugesan [Syncfusion]
Replied On May 4, 2015 01:39 PM

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




Vasanth
Replied On May 4, 2015 04:13 PM

your solution works great, thanks!

Saranya CJ [Syncfusion]
Replied On May 5, 2015 02:40 AM

Hi Vasanth,

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

Regards,
Saranya

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.

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.

;