Row Background color is not stable with vertical scrolling

6 posts, 0 answers
  1. Libertad
    Libertad avatar
    53 posts
    Member since:
    Sep 2008

    Posted 19 Mar 2012 Link to this post

    I change the row background in code behind. It works but when I scroll the grid down or up, some other rows background is changed.
    What's my problem?

    private void gridContextMenu_ItemClick(object sender, Telerik.Windows.RadRoutedEventArgs e)
    {
        var menu = (RadContextMenu)sender;
        var clickedItem = e.OriginalSource as RadMenuItem;
        var row = menu.GetClickedElement<GridViewRow>();
     
        if(clickedItem != null && row != null)
        {
            var header = Convert.ToString(clickedItem.Header);
            if (header == Resource.MenuItem_View)
            {
                Foo();
                row.Background = new SolidColorBrush(Color.FromArgb(100, 133, 227, 55));
            }
        }
    }
  2. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 19 Mar 2012 Link to this post

    Hi,

    By default RadGridView has the RowVirtualization turned on. This means that it will reuse/recycle rows when scrolling  in order to boost performance. This is the reason for the side effect you are observing - containers reused for another data item persist their color.  You can easily confirm this by simply temporary disabling row virtualization RadGridView.EnableRowVirtualization = false;

    The solution would be to use row style selector.

    All the best,
    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. DevCraft banner
  4. Libertad
    Libertad avatar
    53 posts
    Member since:
    Sep 2008

    Posted 19 Mar 2012 Link to this post

    I set RowVirtualization to false but the background color is not stable still.
  5. Sunil
    Sunil avatar
    7 posts
    Member since:
    Sep 2014

    Posted 16 Sep 2014 in reply to Pavel Pavlov Link to this post

    RadGridView.EnableRowVirtualization = false

    this is working in my code.
    Now cell background color is stable


    Thank you :)
  6. Sunil
    Sunil avatar
    7 posts
    Member since:
    Sep 2014

    Posted 20 Oct 2014 in reply to Pavel Pavlov Link to this post

    Hi,
    this is solved my problem.
    RadGridView.EnableRowVirtualization = false



    this is working in my code.

    Now cell background color is stable.

    But now i am facing another problem while filtering records on clicking on filter icon and again remove the filter then background color of cell is maintain but another row(just above the colored cell) of the grid also get colored with same background color.


    for this problem i have a solution:
    Take a Label inside DataTemplate and mark the label background color according to the condition on code behind on RowLoaded event.

    and this fixed my problem

    <telerik:GridViewDataColumn Header="Common Fields" UniqueName="CommonFields" Width="*" DataMemberBinding="{Binding CommonField}">
                        <telerik:GridViewDataColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel>
                                    <Label Name="lblCommonFields" BorderThickness="1" Content="{Binding CommonField}" FontWeight="Normal" ></Label>
                                </StackPanel>
                            </DataTemplate>
                        </telerik:GridViewDataColumn.CellTemplate>
      
                    </telerik:GridViewDataColumn>
      Code Behind:
    private void grdComparisonWindow_RowLoaded(object sender, Telerik.Windows.Controls.GridView.RowLoadedEventArgs e)
            {
                GridViewRow row = e.Row as GridViewRow;
    EDI271Comparison objEV = e.Row.DataContext as EDI271Comparison;
    for (int index = 0; index < row.Cells.Count; index++)
                    {
                        if (row.Cells[index].Column.UniqueName.Equals("CommonFields", StringComparison.OrdinalIgnoreCase))
                        {
                            StackPanel stckpnl = (StackPanel)row.Cells[index].FindChildByType<StackPanel>();
                            lblCommonFields = (System.Windows.Controls.Label)stckpnl.Children[0];
                        }
    }
    if (!string.IsNullOrEmpty(careValue) && !string.IsNullOrEmpty(ediValue))
                            {
                                if (!string.Equals(careValue, ediValue))
                                {
                                    lblCommonFields.Background = Brushes.Yellow;
                                }
                            }

    This is my solution.

    If you can suggest any inbuild solution of telerik grid for this your welcome.
  7. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 21 Oct 2014 Link to this post

    Hi,

    Generally, we do not recommend working with the visual elements in RadGridView as it is a virtualized control and its elements are reused as they are brought in and out the view area. You can check our online documentation explaining how the UI virtualization works. 

    As to the specific problem you meet while having the row virtualization disabled, may I ask you to open a new support tickets and send us a demo project which we can check locally?

    Regards,
    Dimitrina
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
Back to Top
DevCraft banner