This is a migrated thread and some comments may be shown as answers.

Format Grid Columns make string!

1 Answer 103 Views
GridView
This is a migrated thread and some comments may be shown as answers.
noman nadeem
Top achievements
Rank 1
noman nadeem asked on 27 Aug 2009, 06:10 PM
hi!
i want to format telerik grid column , i have text in the grid but its shows like that 00000000AB in this format i want to show only AB
which format should i used please reply me how to handle it.

i used code like that :
<telerik:GridViewDataColumn HeaderText="Sub Heading" DataMemberBinding="{Binding subheadingname3}" DataFormatString="{}{0:G}" TextAlignment="Left" />



Regards,
noman

1 Answer, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 28 Aug 2009, 05:52 AM
Hi noman nadeem,

Well, the easiest way to do that is to introduce a new property for your data objects called ShortHeading and bind to that property instead. I far as I know it is not possible to return a substring using format strings.

Another approach is to create a simple CellTemplate for that column and use a converter to trim the original value. Here is a sample:

public class SubstringConverter : IValueConverter  
{  
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)  
    {  
        string stringValue = System.Convert.ToString(value);  
        int lenght = System.Convert.ToInt32(parameter);  
          
        if(lenght >= 0)  
            return stringValue.Substring(0, Math.Abs(lenght));  
        else 
            return ReverseString(ReverseString(stringValue).Substring(0, Math.Abs(lenght)));  
    }  
 
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)  
    {  
        throw new NotImplementedException();  
    }  
 
    public static string ReverseString(string strinToReverse)  
    {  
        char[] stringArray = strinToReverse.ToCharArray();  
        Array.Reverse(stringArray);  
        return new string(stringArray);  
    }  

<UserControl.Resources> 
    <local:SubstringConverter x:Key="StringConverter"/>  
</UserControl.Resources> 
<Grid x:Name="LayoutRoot" Background="White">  
    <telerikGrid:RadGridView Name="RadGridView1" AutoGenerateColumns="False">  
        <telerikGrid:RadGridView.Columns> 
            <telerikGrid:GridViewDataColumn HeaderText="Sub Heading"   
                                        DataMemberBinding="{Binding subheadingname3}" 
                                        TextAlignment="Left">  
                <telerikGrid:GridViewDataColumn.CellTemplate> 
                    <DataTemplate> 
                        <TextBlock Text="{  
                            Binding Path=subheadingname3,   
                            Converter={StaticResource StringConverter},   
                            ConverterParameter=-2}"/> 
                    </DataTemplate> 
                </telerikGrid:GridViewDataColumn.CellTemplate> 
            </telerikGrid:GridViewDataColumn> 
        </telerikGrid:RadGridView.Columns> 
    </telerikGrid:RadGridView> 
</Grid> 

The ConverterParameter controls which part of a string is returned. When you specify a value of -2 the last two characters will be returned.

Hope this helps.

Regards,
Milan
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
GridView
Asked by
noman nadeem
Top achievements
Rank 1
Answers by
Milan
Telerik team
Share this question
or