Change selected grid view row color.

12 posts, 1 answers
  1. Raymond
    Raymond avatar
    134 posts
    Member since:
    Jul 2010

    Posted 05 Nov 2010 Link to this post

    Hi,

    Could you please provide me any solution how to change selected grid view row color using Visual Style Builder.

    I'd like to customize it and don't know how to handle this problem.

    I was browsing it for a long time with out any result ... 

    Thanks in advance,

    Regards.

  2. Answer
    Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 05 Nov 2010 Link to this post

    Hello Raymond,

    If you just want to do this using the Style builder please ask this question here.
    But you can achieve the same thing programmatically by handling the RowFormatting event.
    Please take a look at this example:
    void radGridView1_RowFormatting(object sender, RowFormattingEventArgs e)
    {
        if (e.RowElement.IsSelected)
        {
            e.RowElement.BackColor = Color.Red;
        }
        else
        {
            e.RowElement.ResetValue(LightVisualElement.BackColorProperty, Telerik.WinControls.ValueResetFlags.Local);
        }
    }

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Ram
    Ram avatar
    97 posts
    Member since:
    Jun 2011

    Posted 21 Jun 2011 Link to this post

    Hi
    Every one I'm new to telerik Controls.
    I'm  using RadGrid .
    But where can I see the Rowformatting event in my grid.I did't find any such event in properties.
    Please help me out.
    I want to set the selected item forecolor.
    Please help me how to handle Rowformating and Cellformating events.
  5. Ivan Petrov
    Admin
    Ivan Petrov avatar
    701 posts

    Posted 24 Jun 2011 Link to this post

    Hello Ram,

    Thank you for writing.

    The solution given by Emanuel is the right approach in this scenario. In regards to your second issue I tested your current version of the controls (2010 Q2 SP2) and both CellFormatting and RowFormatting can be found in the events tab of the VS property grid when you select a RadGridView. The other way to subscribe to the events is by code as in the following code snippet:

    this.radGridView1.CellFormatting += new CellFormattingEventHandler(radGridView1_CellFormatting);
    this.radGridView1.RowFormatting += new RowFormattingEventHandler(radGridView1_RowFormatting);

    I hope this will help. If you have further questions, feel free to ask.

    Regards,
    Ivan Petrov
    the Telerik team
    Q1’11 SP1 of RadControls for WinForms is available for download; also available is the Q2'11 Roadmap for Telerik Windows Forms controls.
  6. Kamelia
    Kamelia avatar
    7 posts
    Member since:
    Sep 2014

    Posted 23 Sep 2014 Link to this post

    Hi , could you help me please?
    I want the color of any rows which was selected before., changes 
    in a way that by looking at my gridview , I can see the which of the rows has been selected ones before!
    thank you 
  7. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 24 Sep 2014 Link to this post

    Hi Kamelia,

    Thank you for writing.

    You can use the CurrentRowChanged event of RadGridView in order to detect when a row is being selected and store this information somewhere (for example in the row's Tag). Then, use this information in the RowFormatting event to determine whether to colorize the row or not. Here is an example:
    void radGridView1_RowFormatting(object sender, RowFormattingEventArgs e)
    {
        if (e.RowElement.RowInfo.Tag == "Selected")
        {
            e.RowElement.BackColor = Color.Lime;
            e.RowElement.DrawFill = true;
        }
        else
        {
            e.RowElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
            e.RowElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
        }  
    }
     
    void radGridView1_CurrentRowChanged(object sender, CurrentRowChangedEventArgs e)
    {
        e.CurrentRow.Tag = "Selected";
    }

    I hope this helps.

    Regards,
    Stefan
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  8. Kamelia
    Kamelia avatar
    7 posts
    Member since:
    Sep 2014

    Posted 29 Sep 2014 Link to this post

    Hi Stefan,
    Thank you for replying.

    it worked. I have another question here:
    whenever the gridview is binding , at the very begining , the first row of it is in selected mode!
    I do not want it to be selected , I used this code in the page load:
     radGridView1.Rows[0].IsSelected = false;

    but  the lime color that you had given to the selected rows , is on the first row yet!
    could you help me with that please?

    Thank you in advance.


  9. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 30 Sep 2014 Link to this post

    Hi Kamelia,

    Besides removing the selected row, you should remove the current row as well:
    radGridView1.CurrentRow = null;

    Off topic, may I please ask you to separate the questions that are not related in separate thread, so the forums remain easy to search and navigate. Thank you for the understanding.

    Regards,
    Stefan
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  10. Kamelia
    Kamelia avatar
    7 posts
    Member since:
    Sep 2014

    Posted 30 Sep 2014 Link to this post

    Hi again
    firstly, I have to apologize for not knowing the rules correctly
    I thought my question is related , so I asked it here.

    secondly, I add  "radGridView1.CurrentRow = null;  "

    but it makes an error in :

    private void grdKartablDaftar_SelectionChanged(object sender, EventArgs e)
            {
                 GridViewRowInfo ri = grdKartablDaftar.CurrentRow;
                int rowIndex = ri.Index;
                if (rowIndex >= 0)
                {
                    bool selectedRowState = grdKartablDaftar.Rows[rowIndex].IsSelected;
                    
                }



    and the error is in the secend line :  int rowIndex = ri.Index;

    : "Object reference not set to an instant f an object"
                
  11. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 30 Sep 2014 Link to this post

    Hi,

    Well, when we set the current row to null in order to clear it, if is understandable that the CurrentRow property will return null, hence the null reference exception. 

    By the way, what is the purpose of this code that you pasted?

    Regards,
    Stefan
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  12. Sebastian
    Sebastian avatar
    4 posts
    Member since:
    Mar 2014

    Posted 22 Oct 2014 in reply to Stefan Link to this post

    I have not found anywhere the way to change row's background color when mouse is over the row...
    Is there any way?
  13. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 22 Oct 2014 Link to this post

    Hello Sebek,

    To do this in code, you can use the MouseMove event of RadGridView. Here is an example:
    GridCellElement lastHoveredCell;
     void radGridView1_MouseMove(object sender, MouseEventArgs e)
     {
         GridCellElement hoveredCell = radGridView1.ElementTree.GetElementAtPoint(e.Location) as GridCellElement;
     
         if (hoveredCell != null)
         {
             if (hoveredCell.RowElement.IsCurrent)
             {
                 hoveredCell.RowElement.DrawFill = true;
                 hoveredCell.RowElement.BackColor = Color.Yellow;
                 hoveredCell.RowElement.GradientStyle = GradientStyles.Solid;
             }
             else if (lastHoveredCell != null)
             {
                 lastHoveredCell.RowElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
                 lastHoveredCell.RowElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
                 lastHoveredCell.RowElement.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local);
             }
     
             lastHoveredCell = hoveredCell;
         }
     }

    I hope this helps.

    Regards,
    Stefan
    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
UI for WinForms is Visual Studio 2017 Ready