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

[Solved] Change RadGridView header programmatically

6 Answers 544 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Enill
Top achievements
Rank 1
Enill asked on 15 Apr 2010, 01:35 AM
Hi,
i have a simple question actually but i can't figure it out since i'm pretty new with all the Telerik controls.

Lets say i am displaying a 20 columns database(dynamically loaded) in a RadGridView, i would like to know how we can change the header backcolor of the 4 first columns programmatically(or with a style?). By default it is black, but is it possible to have them with a green backcolor for example and all the other stay black?

Thanks in advance!

6 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 15 Apr 2010, 02:38 PM
Hi Enill,

As you want to change the background of more than one columns' headers, it is better to create a Resource and afterwards to use it in the Property "HeaderCellStyle" of the GridViewDataColumn as a StaticResource. 

The definition of the Resource is:

<UserControl.Resources>
  <Style x:Key="ColumnHeaderCellStyle" TargetType="telerikGridView:GridViewHeaderCell" >
     <Style.Setters>
    <Setter Property="Background" Value="Green"/>
     </Style.Setters>
   </Style>
</UserControl.Resources>

Thus the definition of the specified column is:

<telerik:GridViewDataColumn  Header ="Name" 
                    HeaderCellStyle="{StaticResource   ColumnHeaderCellStyle}"                       DataMemberBinding="{Binding Name}" 
                    Background="Bisque">

Please, make sure that you add the right assembly, containing the definition of the GridViewHeaderCell, which is:

xmlns:telerikGridView="clr-namespace:Telerik.Windows.Controls.GridView;assembly=Telerik.Windows.Controls.GridView"

Greetings,
Maya
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Ifeanyi
Top achievements
Rank 1
answered on 21 Sep 2010, 01:09 AM
I have a similar problem.

We are doing a new silverlight project using RadGridView.  Our requirement states that we need to load the entire literals on our forms including the header of a RadGridview from a WCF webs service. The other controls are loading just fine but the RadGridView header does not.  My fear is that the header is constructed before the service returns (WCF RIA Service).

is there any way I can change this header content after it has loaded because the service is not guarantied to be available at that time

Thanks in advance
0
Milan
Telerik team
answered on 21 Sep 2010, 07:20 AM

Hello Ifeanyi,

Could you please provide us with more information? What part of the header is not rendered correctly? 

There should be no problem to change the Header content at runtime - you just need to get a reference to a column (using the Columns collection) and set its Header property.


All the best,
Milan
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Ifeanyi
Top achievements
Rank 1
answered on 21 Sep 2010, 12:21 PM
Thank you Milan,

We were binding the RadGridView Header to a property in our view model as below:

            <telerik:RadGridView.Columns>
                <telerik:GridViewToggleRowDetailsColumn />
                <telerik:GridViewDataColumn Header="{Binding Codes}" Width="100" DataMemberBinding="{Binding CodesValues, Mode=TwoWay}" />
                <telerik:GridViewDataColumn Header="{Binding Description}" Width="*"  DataMemberBinding="{Binding DescriptionValues, Mode=TwoWay}" />
                <telerik:GridViewDataColumn Header="{Binding ShortName}" Width="200"  DataMemberBinding="{Binding ShortNameValues, Mode=TwoWay}" />
                <telerik:GridViewDataColumn Header="{Binding Grades}" Width="200"  DataMemberBinding="{Binding GradesValues, Mode=TwoWay}" />
                <telerik:GridViewDataColumn Header="{Binding Currency}" Width="150"  DataMemberBinding="{Binding CurrencyValues, Mode=TwoWay}" />
            </telerik:RadGridView.Columns>

The Binding is on a View Model on another assembly.

How when the RadGridview loads, the column header does not show the properties in the binding. But the Grid is loaded with the CodesValues, DescriptionValues etc. 

Can I use the column collection you suggested using binding and MVVM?

Thanks in advance


0
Maya
Telerik team
answered on 22 Sep 2010, 11:20 AM
Hello Ifeanyi,

The way you may use the Columns Collection of the RadGridView in order to get the Headers is like:

this.RadGridView1.Columns[index].Header
 or
this.RadGridView1.Columns[UniqueName].Header

Thus you may set the Header of each of the columns programmatically.
Another possible approach may be to expose your ViewModel as a Resource and define it as a StaticResource in the definition of the Header:
<UserControl.Resources>
       <my:MyViewModel x:Key="MyViewModel"/>
</UserControl.Resources>

<telerik:GridViewDataColumn DataMemberBinding="{Binding Name}" Header="{Binding             Header1, Source={StaticResource MyViewModel}}"/>


Regards,
Maya
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Ifeanyi
Top achievements
Rank 1
answered on 22 Sep 2010, 12:11 PM
Thanks so much Maya,

The Static Resource Option satisfied my requirement.
Tags
GridView
Asked by
Enill
Top achievements
Rank 1
Answers by
Maya
Telerik team
Ifeanyi
Top achievements
Rank 1
Milan
Telerik team
Share this question
or