Row Style in code behind

10 posts, 0 answers
  1. Srini
    Srini avatar
    41 posts
    Member since:
    Jul 2009

    Posted 11 Sep 2009 Link to this post

    Hi

    I want to add row style in code behind

    I'm doing it in OnRowLoaded Event

    Like e.Row.style ?????

    Can anybody help me out from this
  2. Rossen Hristov
    Admin
    Rossen Hristov avatar
    2478 posts

    Posted 11 Sep 2009 Link to this post

    Hi Srini,

    You can set properties of the row directly in the RowLoaded event.

    If you want to use styles you should work with the RowStyle and AlternatingRowStyle of the grid. Something like this:

    using System.Windows; 
    using System.Windows.Controls; 
    using System.Windows.Media; 
    using Telerik.Windows.Controls.GridView; 
     
    namespace TicketID_240045_CellBackgroundBaseOnAValue 
        public partial class MainPage : UserControl 
        { 
            public MainPage() 
            { 
                this.InitializeComponent(); 
                this.playersGrid.ItemsSource = Club.GetPlayers(); 
     
                this.playersGrid.RowStyle = new Style(typeof(GridViewRow)); 
                this.playersGrid.RowStyle.Setters.Add(new Setter(Control.BackgroundProperty 
                    , new SolidColorBrush(Colors.Yellow))); 
     
                this.playersGrid.UseAlternateRowStyle = true
                this.playersGrid.AlternateRowStyle = new Style(typeof(GridViewRow)); 
                this.playersGrid.AlternateRowStyle.Setters.Add(new Setter(Control.BackgroundProperty 
                    , new SolidColorBrush(Colors.Green))); 
            } 
     
            private void OnRowLoaded(object sender, RowLoadedEventArgs e) 
            { 
                if (e.Row is GridViewRow) 
                { 
                    e.Row.Foreground = new SolidColorBrush(Colors.Red); 
                } 
            } 
        } 

    I hope this helps.

    Kind regards,
    Ross
    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.
  3. DevCraft banner
  4. Srini
    Srini avatar
    41 posts
    Member since:
    Jul 2009

    Posted 11 Sep 2009 Link to this post

    Thanks for your Prompt reply...
    but how to restrict the mouseOver and Selected Color change.
  5. Rossen Hristov
    Admin
    Rossen Hristov avatar
    2478 posts

    Posted 11 Sep 2009 Link to this post

    Hello Srini,

    If you want to do such modifications you will have to restyle RadGridView in Expression Blend.

    Here is the help topic that describes how to do that.

    Sincerely yours,
    Ross
    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.
  6. Srini
    Srini avatar
    41 posts
    Member since:
    Jul 2009

    Posted 11 Sep 2009 Link to this post

    it is applying for the whole grid but i want it for particular row.

    private void rgvTripHistory_RowLoaded(object sender, RowLoadedEventArgs e)
            {
                if (e.DataElement != null)
                {
                    if (((CustomerTripHistoryBO)e.DataElement).Dispute)
                    {
                        e.Row.Cells[1].IsEnabled = false;
                        e.Row.Background = new SolidColorBrush(Color.FromArgb(255, 117, 213, 126));
                    }
                    else
                    {

                            I want these rows to be in Some Selected color as well It should not trigger for Mouse over and Selected
                        this.rgvTripHistory.RowStyle = new Style(typeof(GridViewRow));
                        this.rgvTripHistory.RowStyle.Setters.Add(new Setter(Control.BackgroundProperty
                            , new SolidColorBrush(Colors.Yellow)));


                        //this.rgvTripHistory.AlternateRowStyle = new Style(typeof(GridViewRow));
                        //this.rgvTripHistory.AlternateRowStyle.Setters.Add(new Setter(Control.BackgroundProperty, new                     SolidColorBrush(Colors.Green)));

                        e.Row.Background = new SolidColorBrush(Colors.Red);

                    }
                }
            }
  7. Rossen Hristov
    Admin
    Rossen Hristov avatar
    2478 posts

    Posted 11 Sep 2009 Link to this post

    Hello Srini,

    In this case you will need to do 2 things.

    1. Style the grid like I said in my previous post. You will remove the MouseOver and Selected colors for the whole grid. Now you will have a grid without mouse over and selection effects.

    2. Attach to the RowLoaded event, check the DataElement and directly set e.Row.Background to the appropriate color. Now you will have different rows in different color depending on their type and you will not have the selection and mouse over overriding there color.

    I hope this helps.

    Best wishes,
    Ross
    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.
  8. Srini
    Srini avatar
    41 posts
    Member since:
    Jul 2009

    Posted 11 Sep 2009 Link to this post

    Thanks for ur effort. I have already Designed it in blend. Actually my requirment is i have some Checked CheckBox rows which i should show in selected color remaing grid rows should behave normally like what i designd in blend, for that i have added a UserControl:Resource now everthing is working fine but, When i try to Mouse over or select the Checked row it is behaving like a default telerik grid color.
    This is what i did 

    private void rgvTripHistory_RowLoaded(object sender, RowLoadedEventArgs e)
            {
                if (e.DataElement != null)
                {
                    if (((CustomerTripHistoryBO)e.DataElement).Dispute)
                    {
                        e.Row.Cells[1].IsEnabled = false;
                        e.Row.Background = new SolidColorBrush(Color.FromArgb(255, 117, 213, 126));
    //Here Mouse Over and select Color behave default. So either i want to change the MouseOver and Selected color OR i want to //restrict it.
                    }
                    else
                    {
                        e.Row.Style = (Style)this.Resources["GridViewRowStyle1"];
                    }
                }
            }
  9. Rossen Hristov
    Admin
    Rossen Hristov avatar
    2478 posts

    Posted 11 Sep 2009 Link to this post

    Hi Srini,

    Can you please send us a small sample project that demonstrates what you have done.

    We will do our best to come up with a workaround, but I do not think that disabling the mouse over and selection visual states for certain rows only would be possible.

    Sincerely yours,
    Ross
    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.
  10. Kristopher
    Kristopher avatar
    5 posts
    Member since:
    Feb 2011

    Posted 31 Mar 2011 Link to this post

    Hello,

    How can i change the selected row color on expand command?
  11. Ivan Ivanov
    Admin
    Ivan Ivanov avatar
    1127 posts

    Posted 31 Mar 2011 Link to this post

    Hello Kristopher,

    I have prepared an example that describes a possible approach to your case. Of course you can just set the Background property, instead of applying a style, but utilizing this approach will enable you to reach a higher level of customization. 

    Greetings,
    Ivan Ivanov
    the Telerik team
Back to Top
DevCraft banner