Loop rows and change background color

9 posts, 0 answers
  1. Danilo
    Danilo avatar
    47 posts
    Member since:
    Sep 2012

    Posted 18 Mar 2013 Link to this post

    Hello, I'm loonking for a way to change the background color of some rows  in a GridView after databinding.
    Currently I use the loadComplete Event to loop the rows:
    RadGridView table = sender as RadGridView;
            
            foreach (GridViewRowInfo r in table.Rows)
           {
     
           }
    Unfortunately I was not able to find a way to change the background color of the GridVieRowInfo Elements.
    It is important that I can change the backround by looping because the rows which should get colored are not periodical.
    Can someone tell me how to do that?
    Any help very apreciated. Thank you
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 18 Mar 2013 Link to this post

    Hi,

    You can customize the rows in RowFormatting event as shown below.
    C#:
    protected void radGridView1_RowFormatting(object sender, RowFormattingEventArgs e)
    {
        if (e.RowElement.RowInfo.Cells["Columnname"].Value == "text")
        {
            e.RowElement.DrawFill = true;
            e.RowElement.GradientStyle = GradientStyles.Solid;
            e.RowElement.BackColor = Color.Red;
        }
    }

    Thanks,
    Princy
  3. Stefan
    Admin
    Stefan avatar
    2911 posts

    Posted 20 Mar 2013 Link to this post

    Hello guys,

    I can confirm that Princy's suggestion is the right way to go in this case. 

    @Princy - your 
    Telerik Points have been updated for the community effort.

    Regards,
    Stefan
    the Telerik team
    WinForms Q1 2013 boasts PivotGrid, PDF Viewer, Chart enhancements and more. Check out all of the latest highlights.
  4. Mujtaba Ali
    Mujtaba Ali avatar
    6 posts
    Member since:
    Nov 2010

    Posted 12 Jun 2013 Link to this post

    Hi

    I want to change the row's background color outside of RowFormatting event, is it possible?

    Regards,

    Mujtaba Ali Panjwani
  5. Stefan
    Admin
    Stefan avatar
    2911 posts

    Posted 17 Jun 2013 Link to this post

    Hello Mujtaba Ali,

    Thank you for writing.

    The correct place to change the Backcolor of a row is the RowFormatting event of RadGridView. Could you please provide me with information why you do not want to use this event?

    Regards,
    Stefan
    Telerik
    RadChart for WinForms is obsolete. Now what?
  6. Mujtaba Ali
    Mujtaba Ali avatar
    6 posts
    Member since:
    Nov 2010

    Posted 17 Jun 2013 Link to this post

    Hi

    Thanks for the reply.

    I wanted to change the row's background color when a row is right clicked. It would be the best to change the backcolor directly but currently I have to store row index and then call the update method to trigger the formatting event which, of course, doesn't look like a good idea.

    Regards,

    Mujtaba Ali Panjwani
  7. Stefan
    Admin
    Stefan avatar
    2911 posts

    Posted 20 Jun 2013 Link to this post

    Hello Mujtaba Ali,

    To do that you can use the CellClick event of the grid, where you can get the clicked cell and its row. Set the row Tag to something and use this Tag in the formatting event to identify the rows you need to style. Here is an example:
    void radGridView1_CellClick(object sender, GridViewCellEventArgs e)
    {
        e.Row.Tag = "something";
    }
     
    void radGridView1_RowFormatting(object sender, RowFormattingEventArgs e)
    {
        if (e.RowElement.RowInfo.Tag != null)
        {
            e.RowElement.BackColor = Color.Red;
            e.RowElement.GradientStyle = GradientStyles.Solid;
            e.RowElement.DrawFill = true;
        }
        else
        {
            e.RowElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
            e.RowElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
            e.RowElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local);
        }
    }

    I hope this helps.
     
    Regards,
    Stefan
    Telerik
    RadChart for WinForms is obsolete. Now what?
  8. Mujtaba Ali
    Mujtaba Ali avatar
    6 posts
    Member since:
    Nov 2010

    Posted 20 Jun 2013 Link to this post

    Hi

    Thanks for the reply. They are very helpful.

    I have done it approximately the same style but I also had to call
    grdPrvw.TableElement.Update(GridUINotifyAction.StateChanged)

    But I still think that there could be better ways to change the appearance instead of using only RowFormatting event.

    Also I had to do some workaround to get if right button was clicked or left using Grid's "MouseClick" event.

    Does the RadGrid have any generic way to detect this within CellClick event.

    Thanks for your time.

    Regards,

    Mujtaba Ali Panjwani
  9. Stefan
    Admin
    Stefan avatar
    2911 posts

    Posted 25 Jun 2013 Link to this post

    Hello Danilo,

    Thank you for writing back.

    1. The RowFormatting event is the right place to introduce appearance modification for the rows in the grid as the control uses UI virtualization for its cells. More information is available here: Grid Explained: RadGridView for WinForms virtualization and its implications. The case with CellElement and RowElement.

    2. You can move the logic from the CellClick to the MouseDown event. Here is how it can look like:
    void radGridView1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Right)
        {
            GridDataCellElement dataCell = radGridView1.ElementTree.GetElementAtPoint(e.Location);
     
            if (dataCell != null)
            {
                dataCell.RowInfo.Tag = "something";
            }
        }
    }

    I hope this helps.
     
    Regards,
    Stefan
    Telerik
    RadChart for WinForms is obsolete. Now what?
Back to Top