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

Change cell using CellFormatting

5 Answers 262 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Geir Aage Amundsen
Top achievements
Rank 1
Geir Aage Amundsen asked on 25 Feb 2008, 07:30 AM

Question regarding CellFormatting

I have just bought a couple of licenses of radcontrols. And I must say; I am very impressed! It helps me save loads of time.

I have a radgrid filled with data (like this):

Name     Bit (should be hidden)      Phone

John        1                                    22445566
Mike       0                                    44556677
Joan        1                                    44556677

And I want to change the background-color on Name-cell if Bit is set to 1. The background color for the cell with John and Joan should be yellow. And Mike should stay as it is.

This is normally functionality that I would solve using the OnItemDataBound-event. But now I guess I have to use the CellFormatting-event instead.

I figured out that I could not grab the value of Bit, because the column is hidden (radgridDialog.Columns["myBitField"].IsVisible = false;)

So then I tried to prefix my name-column from my stored procedure. So John and Joan became <pre>John and <pre>Joan. Not the nicest solution I have seen. But I was starting to get desperate. Do anyone have an better idea?

The logic almost work. The only thing I'm having problems with now is to change the value of the cell. My function looks like this:

void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)  
{  
        // exclude header element in the data column                         
        if (e.CellElement.ColumnInfo is GridViewDataColumn && !(e.CellElement.RowElement is GridHeaderRowElement))  
        {  
             GridViewDataColumn column = (GridViewDataColumn)e.CellElement.ColumnInfo;  
 
              if (column.DataField == "Name")  
              {  
                  if (e.CellElement.Value.ToString().StartsWith("<pre>"))  
                  {  
                         Font font = e.CellElement.Font;  
 
    //What should the line below be? I see that e.CellElement.Value is readonly  
                         //e.CellElement.SetValue(Telerik.WinControls.RadProperty rd, (object)e.CellElement.Value.ToString().Replace("<pre>", ""));  
                         e.CellElement.Font = font;  
                         e.CellElement.BackColor = Color.Yellow;  
                         e.CellElement.ForeColor = Color.Tomato;  
                  }  
             }  
      }  
}  
 
 

Thanks

5 Answers, 1 is accepted

Sort by
0
Geir Aage Amundsen
Top achievements
Rank 1
answered on 25 Feb 2008, 07:52 AM
Hmm.. Found the solution.

void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)  
         {  
             // exclude header element in the data column                         
             if (e.CellElement.ColumnInfo is GridViewDataColumn && !(e.CellElement.RowElement is GridHeaderRowElement))  
             {  
                 GridViewDataColumn column = (GridViewDataColumn)e.CellElement.ColumnInfo;  
 
                 if (column.DataField == "Name")  
                 {  
                     if (e.CellElement.Value.ToString().StartsWith("<pre>"))  
                     {  
                         Font font = e.CellElement.Font;  
 
                         Telerik.WinControls.RadProperty radProperty = e.CellElement.GetRegisteredRadProperty("Text");  
 
                         e.CellElement.SetValue(radProperty, (object)e.CellElement.Value.ToString().Replace("<pre>"""));  
                         e.CellElement.Font = font;  
                         e.CellElement.BackColor = Color.Yellow;  
                         e.CellElement.ForeColor = Color.Tomato;  
                     }  
                 }  

Please give some feedback if it can be done a "nicer" way.

Thanks
0
Accepted
Jack
Telerik team
answered on 25 Feb 2008, 08:37 AM
Hello Geir Aage Amundsen,

Thank you for contacting us.

You are on the right way. The cell value can be accessed through the Cells collection of the GridViewRowInfo, regardless of the visibility of the specified column. Consider the code snippet below:

void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e) 
    // exclude header element in the data column     
    if (e.CellElement.ColumnInfo is GridViewDataColumn && !(e.CellElement.RowElement is GridHeaderRowElement)) 
    { 
        GridViewDataColumn column = (GridViewDataColumn)e.CellElement.ColumnInfo; 
        if (column.DataField == "Name"
        { 
            if ((bool)e.CellElement.RowInfo.Cells["Bit"].Value) 
            { 
                if (!e.CellElement.RowInfo.IsCurrent && !e.CellElement.RowInfo.IsSelected) 
                { 
                    e.CellElement.BackColor = Color.Yellow; 
                    e.CellElement.DrawFill = true
                } 
                else 
                { 
                    e.CellElement.DrawFill = false
                } 
                e.CellElement.ForeColor = Color.Tomato;  
            } 
        } 
    } 

I hope this helps. Let us know, if you need further assistance.

Kind regards,
Jack
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
0
Geir Aage Amundsen
Top achievements
Rank 1
answered on 25 Feb 2008, 09:38 AM
Thanks for the quick reply! It was just what I was looking for. It felt good to remove the "hack solution" that I made.
0
Dan
Top achievements
Rank 2
answered on 18 Apr 2008, 08:25 PM
Did you try scrolling after using this method.  I get a smearing behavior.  It starts out with the correct rows colored but then onscroll it causes the color to run down the grid.

0
Jack
Telerik team
answered on 21 Apr 2008, 11:22 AM
Hi Dan,

Thank you for pointing this out.

You are right, in this particular scenario the grid behaves incorrectly when it is scrolled. You should always restore the old values when changing some properties through the CellFormatting event.

The following code corrects the issue:

void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e) 
    // exclude header element in the data column      
    if (e.CellElement.ColumnInfo is GridViewDataColumn && !(e.CellElement.RowElement is GridTableHeaderRowElement)) 
    { 
        GridViewDataColumn column = (GridViewDataColumn)e.CellElement.ColumnInfo; 
        if (column.FieldName == "Name"
        { 
            if ((bool)e.CellElement.RowInfo.Cells["Bit"].Value) 
            { 
                if (!e.CellElement.RowInfo.IsCurrent && !e.CellElement.RowInfo.IsSelected) 
                { 
                    e.CellElement.BackColor = Color.Yellow; 
                    e.CellElement.DrawFill = true
                } 
                else 
                { 
                    e.CellElement.DrawFill = false
                } 
                e.CellElement.ForeColor = Color.Tomato; 
            } 
            else 
            { 
                e.CellElement.DrawFill = false
            } 
        } 
    }  

I hope this helps. Do not hesitate to contact me, if you have other questions.

Regards,
Jack
the Telerik team

Instantly find answers to your questions at the new Telerik Support Center
Tags
GridView
Asked by
Geir Aage Amundsen
Top achievements
Rank 1
Answers by
Geir Aage Amundsen
Top achievements
Rank 1
Jack
Telerik team
Dan
Top achievements
Rank 2
Share this question
or