This is a migrated thread and some comments may be shown as answers.

Change Row Color When checkbox is checked

15 Answers 760 Views
GridView
This is a migrated thread and some comments may be shown as answers.
jerry
Top achievements
Rank 1
jerry asked on 05 May 2009, 01:28 PM
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

15 Answers, 1 is accepted

Sort by
0
gerbrand
Top achievements
Rank 2
answered on 06 May 2009, 08:38 AM
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)
0
Shinu
Top achievements
Rank 2
answered on 06 May 2009, 09:14 AM
Hi Jerry,

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

Shinu.
0
jerry
Top achievements
Rank 1
answered on 06 May 2009, 01:22 PM
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
0
gerbrand
Top achievements
Rank 2
answered on 06 May 2009, 03:13 PM
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;






0
jerry
Top achievements
Rank 1
answered on 06 May 2009, 03:54 PM
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
0
jerry
Top achievements
Rank 1
answered on 06 May 2009, 03:59 PM
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?

0
gerbrand
Top achievements
Rank 2
answered on 06 May 2009, 07:36 PM
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


0
jerry
Top achievements
Rank 1
answered on 06 May 2009, 07:44 PM
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
0
gerbrand
Top achievements
Rank 2
answered on 06 May 2009, 08:12 PM
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


0
jerry
Top achievements
Rank 1
answered on 06 May 2009, 08:20 PM
gerbrand,
Thank you so much for taking the time to help me.

Jerry
0
gerbrand
Top achievements
Rank 2
answered on 06 May 2009, 08:26 PM
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.




0
jerry
Top achievements
Rank 1
answered on 06 May 2009, 08:41 PM
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
0
gerbrand
Top achievements
Rank 2
answered on 06 May 2009, 08:59 PM
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
0
jerry
Top achievements
Rank 1
answered on 06 May 2009, 09:02 PM
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

0
Jack
Telerik team
answered on 07 May 2009, 12:30 PM
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.
Tags
GridView
Asked by
jerry
Top achievements
Rank 1
Answers by
gerbrand
Top achievements
Rank 2
Shinu
Top achievements
Rank 2
jerry
Top achievements
Rank 1
Jack
Telerik team
Share this question
or