Customize text wrapping in RadGridView

1 Answer 54 Views
GridView
Jennifer
Top achievements
Rank 1
Iron
Jennifer asked on 11 Apr 2022, 07:50 PM

Is it possible to customize where it splits the text when you set TextWrapping on a column in a RadGridView?  For example, would it be possible to make it only split the text at spaces and not at other characters?  I have attached screenshots of example text in a RadGridView and the same text in Excel.

 

1 Answer, 1 is accepted

Sort by
0
Dilyan Traykov
Telerik team
answered on 14 Apr 2022, 09:19 AM

Hello Jennifer,

Thank you for the provided images.

Although it is not possible to customize the default text wrapping functionality, you can create custom logic to perform this by implementing an appropriate converter for the column and inserting new lines in the bound text. Here's what I have in mind:

                    <telerik:GridViewDataColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock>
                                <TextBlock.Text>
                                    <MultiBinding Converter="{StaticResource TextNewLineConverter}">
                                        <Binding Path="Name" />
                                        <Binding Path="ActualWidth" RelativeSource="{RelativeSource AncestorType=telerik:GridViewCell}" />
                                    </MultiBinding>
                                </TextBlock.Text>
                            </TextBlock>
                        </DataTemplate>
                    </telerik:GridViewDataColumn.CellTemplate>

You can then define the converter in a similar manner:

    public class TextNewLineConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            var strValue = values[0].ToString();
            var width = (double)values[1];
            if (width < 50)
            {
                var words = strValue.Split(' ').ToList(); 
                words.Insert(1, Environment.NewLine);
                return string.Join("", words);
            }

            return strValue;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

I hope a similar approach would work for you.

Regards,
Dilyan Traykov
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
GridView
Asked by
Jennifer
Top achievements
Rank 1
Iron
Answers by
Dilyan Traykov
Telerik team
Share this question
or