Wrap header text in dynamic structured gridview

7 posts, 0 answers
  1. Mark
    Mark avatar
    61 posts
    Member since:
    Dec 2010

    Posted 21 Mar 2012 Link to this post

    I have a radgridView that is based on  a pivot table, that is the number of columns is based on the query and cannot be defined. To do this I use your data table construct, this works fine however I need to wrap the text in the header, it is up to 500 characters long.

    My latest attempt to fail is trying to apply a style to the header!

    private void GridLoaded(Code.AppMessages.ListCompleteMessage.enMessage eMessage)
    {
        if (eMessage == Code.AppMessages.ListCompleteMessage.enMessage.GenericTable)
        {
            RadGridView oDG = this.RGV1;//default to the element pivot
            if (vm.eCurrentView == ViewModels.TreePivotVM.enView.Label)
            {
                oDG = this.RGVLabel;//get the label pivot grid
            }
            foreach (GridViewColumn oCol in oDG.Columns)
            {
     
                oCol.Header = oCol.Header.ToString().Replace("_", " ");                
                if (oCol.DisplayIndex == 0)
                {                      
                    oCol.Width = Telerik.Windows.Controls.GridViewLength.Auto;
                }
                else
                {                      
                    oCol.MaxWidth = 200;
                    oCol.TextAlignment = TextAlignment.Right;
                    //oCol.HeaderCellStyle = (Style)Application.Current.Resources["TelerikHeaderCellStyle"];                       
                    oCol.CellStyle = (Style)Application.Current.Resources["TelerikReportCellStyle"];                       
                }
            }
        }
    }

    Any suggestions will be greatly appreciated. I would also like to format the numbers in the cells in columns > 0
  2. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 21 Mar 2012 Link to this post

    Hi Mark ,

    Your C# code seems OK. Can you please share the XAML with the style (TelerikHeaderCellStyle) so I can check what might be wrong.
    Regarding the number formatting - data columns in RadGridView expose the DataFormatString property.

    Regards,
    Pavel Pavlov
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  3. Mark
    Mark avatar
    61 posts
    Member since:
    Dec 2010

    Posted 21 Mar 2012 Link to this post

    Certainly, my xaml skills are sadly lacking!

    <!--This style is not working!-->
    <Style x:Key="TelerikHeaderCellStyle" TargetType="telerik:GridViewHeaderCell">     
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="telerik:GridViewHeaderCell">
                    <TextBlock Text="{Binding Column.UniqueName}" TextWrapping="Wrap" Height="Auto" TextAlignment="Left"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>      
        <Setter Property="Height" Value="Auto" />
    </Style>
  4. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 21 Mar 2012 Link to this post

    Hi Mark ,

    Thanks for sending the template! Indeed it could be fixed but would involve much heavier XAML.
    Can you please first check whether this approach would be applicable to your case ?

    Let me know in case this does not work for you.

    Kind regards,
    Pavel Pavlov
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  5. Mark
    Mark avatar
    61 posts
    Member since:
    Dec 2010

    Posted 21 Mar 2012 Link to this post

    Thanks Pavel, that approach will not work as I do not have any columns defined in the xaml (autogenerate) and the text is coming from the database.
  6. Vlad
    Admin
    Vlad avatar
    11100 posts

    Posted 22 Mar 2012 Link to this post

    Hi,

     Auto-generated columns (and the Header property) can be accessed in AutoGeneratingColumn event. You can apply exactly the same approach for these columns as well. 

    Greetings,
    Vlad
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  7. Mark
    Mark avatar
    61 posts
    Member since:
    Dec 2010

    Posted 22 Mar 2012 Link to this post

    Mutter mutter grumble - dammed event does not fire!

    The xaml
    <telerik:RadGridView Grid.Row="1" x:Name="RGVLabel" Style="{StaticResource TelerikGridStyle}"
                                    AutoGenerateColumns="True" AutoGeneratingColumn="ColumnCreate"
                                    ItemsSource="{Binding LabelTable,Mode=OneWay}" IsReadOnly="True" />

    The code behind - breakpoint is not hit!
    private void ColumnCreate(object sender, GridViewAutoGeneratingColumnEventArgs e)
    {
        GridViewColumn oCol = e.Column;
    }

    Note that I also have a post load process initiated by the ViewModel that loops through the created columns (in the first posting) that cannot format the textwrapping on the column header. It seems to me the ColumnCreate event will have the same problem - cannot get at the Column.Header.TextWrapping property to set it.
Back to Top