Change Row Color When checkbox is checked

16 posts, 0 answers
  1. jerry
    jerry avatar
    103 posts
    Member since:
    Feb 2008

    Posted 05 May 2009 Link to this post

    I have a datagridview that has a checkbox column.  I want the row to change color when it's checked and when it's unchecked I want the original color to show up.  What is the best way to do this without using the Themes?  Is that even possible?

    Thank You
    Jerry
  2. gerbrand
    gerbrand avatar
    78 posts
    Member since:
    Mar 2009

    Posted 06 May 2009 Link to this post

    Hi Jerry,

    I think you can catch the state of the checkbox in the radgridview_ValueChanged event.

    something like this:

    if (cell != null || ((GridViewDataColumn)cell.ColumnInfo).FieldName != "checkbox")
    {
         //here you will have to write your code to change the row color.
    }

    (I'm not sure about this)
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 06 May 2009 Link to this post

    Hi Jerry,

    You can also refer the following help articles which explains how to format GridView rows.
    Formatting Rows
    Conditional Formatting Rows

    Shinu.
  5. jerry
    jerry avatar
    103 posts
    Member since:
    Feb 2008

    Posted 06 May 2009 Link to this post

    gerbrand,
    thank you for the reply.
    I have the code like you have it setup it's the code within the if I can't nail down.  I'm able to change the code for the cell with the checkbox but not the entire row.

     public void radGVTables_ValueChanged(object sender, EventArgs e)  
        {  
          if (this.radGVTables.ActiveEditor is RadCheckBoxEditor)  
          {  
            if ((bool)this.radGVTables.ActiveEditor.Value == true)  
            {  
              GridCheckBoxCellElement cbCell = (GridCheckBoxCellElement)sender;  
              cbCell.BackColor = Color.Navy;  
              cbCell.DrawFill = true;  
            } 

    Thank you
    Jerry
  6. gerbrand
    gerbrand avatar
    78 posts
    Member since:
    Mar 2009

    Posted 06 May 2009 Link to this post

    Hi Jerry,

    I think you need to get the rowelement to edit the hole row color:

    something like this:

    var tempRow = cell.RowElement;
    tempRow.BackColor = Color.Red;






  7. jerry
    jerry avatar
    103 posts
    Member since:
    Feb 2008

    Posted 06 May 2009 Link to this post

    gerbrand,
    Thanks for the post that lead me to my answer. 

    Here is what worked

    if (this.radGVTables.ActiveEditor is RadCheckBoxEditor)  
          {  
           
     
            if ((bool)this.radGVTables.ActiveEditor.Value == true)  
            {                                
              GridCheckBoxCellElement cbCell = (GridCheckBoxCellElement)sender;  
              cbCell.RowElement.BackColor = Color.Pink;  
              cbCell.RowElement.DrawFill = true
  8. jerry
    jerry avatar
    103 posts
    Member since:
    Feb 2008

    Posted 06 May 2009 Link to this post

    gerbrand,
    I just discovered another issue.  When I check a row the first row it changes to yellow.  My problem is when I scroll down the first row displayed as part of the gridview stays yellow.  So it seems I'm changing the first row of the grid no matter if the row is checked or not.  How can I have it just highlight the checked row and when I scroll down the rows will change to the correct white color?

  9. gerbrand
    gerbrand avatar
    78 posts
    Member since:
    Mar 2009

    Posted 06 May 2009 Link to this post

    Hi Jerry,

    I tried to make an example of what you said.

    You can download it from my blogpost since I can't add some files here.

    Don't know if this is the result that you wanted.

    gerbrand


  10. jerry
    jerry avatar
    103 posts
    Member since:
    Feb 2008

    Posted 06 May 2009 Link to this post

    gerbrand,
    thank you so much for the demo.   That is so close to what I need. 
    The color doesn't change until I lose focus off of that checkbox.   I saw you are using the RowFormatting event but I need it to happen on the ValueChanged of the grid. 

    Thanks again for taking the time to help me

    Jerry
  11. gerbrand
    gerbrand avatar
    78 posts
    Member since:
    Mar 2009

    Posted 06 May 2009 Link to this post

    Yeah I noticed that also...

    I'll have a look at it, maybe I can improve that... and I will try it with the ValueChanged event.

    gerbrand


  12. jerry
    jerry avatar
    103 posts
    Member since:
    Feb 2008

    Posted 06 May 2009 Link to this post

    gerbrand,
    Thank you so much for taking the time to help me.

    Jerry
  13. gerbrand
    gerbrand avatar
    78 posts
    Member since:
    Mar 2009

    Posted 06 May 2009 Link to this post

    Hi Jerry

    Try this:

    private void rgvTest_ValueChanged(object sender, EventArgs e)
            {
                var cell = sender as GridDataCellElement;
                
                if (cell == null) return;

                var tempRow = cell.RowElement;
                var rowInfo = cell.RowInfo;

                if (!(bool)rowInfo.Cells["checkbox"].Value)
                {
                    tempRow.BackColor = Color.Red;
                    tempRow.DrawFill = true;
                }else
                {
                    tempRow.ResetValue(VisualElement.BackColorProperty);
                }
                
            }

    If you add this to my sample project you will get the result you need I guess.




  14. jerry
    jerry avatar
    103 posts
    Member since:
    Feb 2008

    Posted 06 May 2009 Link to this post

    gerbrand,
    Thank you so much for all of your help.  Do you work for telerik?

    Do you know much about the radmenu?  LOL


    thanks I truely appreciate all your help

    Jerry
  15. gerbrand
    gerbrand avatar
    78 posts
    Member since:
    Mar 2009

    Posted 06 May 2009 Link to this post

    I don't work for telerik... :-)

    But I'm glad I could help you.

    Is your question about RadMenu in the radmenu forum?

    Who knows I can help you also with that :-)

    Gerbrand
  16. jerry
    jerry avatar
    103 posts
    Member since:
    Feb 2008

    Posted 06 May 2009 Link to this post

    Gerbrand thank you so much. 

    It's a difficult question that I'm not sure can even be accomplished.  I need to create 1 menu item that lists years then highlighting those years will show child menues and those child menus need child menues. 

    I just asked a question on someone elses questions because it was some what the same.

    http://www.telerik.com/community/forums/winforms/menus/dynamic-from-database.aspx

    Thanks again
    Jerry

  17. Jack
    Admin
    Jack avatar
    2335 posts

    Posted 07 May 2009 Link to this post

    Hello Jerry,

    You can use the Items collection of RadMenuItem to add child menu items. This can be done also dynamically (e.g when handling the MouseHover event). Please consider the following code snippet:

    RadMenuItem item = new RadMenuItem("Item 1"); 
    item.MouseHover += new EventHandler(item_MouseHover); 
    menu.Items.Add(item); 
     
    void item_MouseHover(object sender, EventArgs e) 
        RadMenuItem item = (RadMenuItem)sender; 
        if (!item.DropDown.IsVisible && item.Items.Count == 0) 
        { 
            item.Items.Add(new RadMenuItem("Item 1.1")); 
            item.Items.Add(new RadMenuItem("Item 1.2")); 
            item.Items.Add(new RadMenuItem("Item 1.3"));                 
        } 
        item.ShowChildItems(); 
     

    I hope this helps. If you need any further assistance, please don't hesitate to write us.

    Regards,
    Jack
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
Back to Top
UI for WinForms is Visual Studio 2017 Ready