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

Conditionally format row based on checkbox value

3 Answers 112 Views
GridView
This is a migrated thread and some comments may be shown as answers.
ewart
Top achievements
Rank 1
ewart asked on 11 Nov 2008, 09:43 PM
ConditionalFormattingObject obj =   new ConditionalFormattingObject("MyCondition", ConditionTypes.Greater, "30"""true);  
obj.CellForeColor = Color.Red;  
obj.RowBackColor = Color.SkyBlue;              
this.radGridView1.Columns[1].ConditionalFormattingObjectList.Add(obj); 

Hi, is it possible to use the conditional format object to format rows based on a checkbox value?  if the value is true I want to apply a format, like the excerpt from the documentation above.  When I've tired with equal/greater than 0/1 it just raises an exception.  If it's not possible in this manner, can you consider adding a IsTrue/IsFalse ConditionType.

I also tried to achieve what I wanted by:
   private void grdTWatchHotList_RowFormatting(object sender, RowFormattingEventArgs e)  
      {  
         if ((bool)e.RowElement.RowInfo.Cells["MarkedForDeath"].Value == true)  
         {  
            e.RowElement.ForeColor = Color.Red;  
            e.RowElement.DrawFill = true;  
         }  
         else 
         {  
            e.RowElement.ResetValue(Telerik.WinControls.VisualElement.ForeColorProperty);  
         }  
 
      } 
 
But this didn't work... initially i through it did but once I added more rows and started scrolling up and down then everything just started getting hilighted in red!

ps the help page ms-help://telerik.winforms.8.0/telerik.winforms/topic24361.html has &amp etc errors in the c# example.

Cheers
  Ewart.

3 Answers, 1 is accepted

Sort by
0
Nick
Telerik team
answered on 13 Nov 2008, 01:54 PM
Hello ewart,

Thank you for your question.

You can use CellFormatting event to apply that kind of formatting:

 private void radGridView1_CellFormatting(object sender, Telerik.WinControls.UI.CellFormattingEventArgs e) 
        { 
            if (e.CellElement.ColumnIndex == 1) { 
                if (e.CellElement.RowIndex>= 0) { 
                    if (e.CellElement.RowInfo.Cells[1].Value   != null && e.CellElement.RowInfo.Cells[1].Value != DBNull.Value) { 
 
                        if ((bool)e.CellElement.RowInfo.Cells[1].Value == true
                        { 
                            e.CellElement.DrawFill = true
                            e.CellElement.BackColor = Color.Beige; 
                        } 
                    } 
                } 
            } 
        } 

Do not hesitate to write me back if you have more questions.

Greetings,
Nick
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
ewart
Top achievements
Rank 1
answered on 13 Nov 2008, 08:54 PM
Thanks Nick, as a want to format a whole row I adapted your example by using

e.CellElement.RowElement.ForeColor = Color.Red;
e.CellElement.RowElement.DrawFill = true;

instead, but it had the same problem I initially raised.. so I tried your exact code however and that also has exactly the same problem as the RowFormatting event - e.g. when I scroll up/down the event fires and starts formatting the cell in random rows regardless of whether they are checked.

What am I missing?

And why would I use the CellFormatting event and not the RowFormatting?  Surely the latter is more efficient - although again not sure why my code didn't work in it as it follows the example in the documentation pretty closely.

Regards
 Ewart.


0
Nick
Telerik team
answered on 14 Nov 2008, 09:45 AM
Hello ewart,

Thank you for contacting me back.

You can use CellFormatting - its performance is comparable. Here is a modified version that resets the background if the checkbox is not checked:

 private void radGridView1_CellFormatting(object sender, Telerik.WinControls.UI.CellFormattingEventArgs e) 
        { 
            if (e.CellElement.ColumnIndex == 1) 
            { 
                if (e.CellElement.RowIndex >= 0) 
                { 
                    if (e.CellElement.RowInfo.Cells[1].Value != null && e.CellElement.RowInfo.Cells[1].Value != DBNull.Value) 
                    { 
 
                        if ((bool)e.CellElement.RowInfo.Cells[1].Value == true
                        { 
                            e.CellElement.DrawFill = true
                            e.CellElement.BackColor = Color.Beige; 
                        } 
                         
                    } 
                    else 
                    { 
                        e.CellElement.DrawFill = false
                    } 
              
 
                } 
            } 
            
        } 
 

Do not hesitate to contact me back if you have further questions.

Best wishes,
Nick
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
GridView
Asked by
ewart
Top achievements
Rank 1
Answers by
Nick
Telerik team
ewart
Top achievements
Rank 1
Share this question
or