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

Conditionally format cell value based on value of another cell

10 Answers 605 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Roberto Wenzel
Top achievements
Rank 2
Roberto Wenzel asked on 01 Sep 2008, 07:52 PM
Hello,
I want to format a cell value in a GridView for WinForms. The format style depends on the value of another column cell.
I tried something like this:

Private

Sub gvMessages_CellFormatting(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.CellFormattingEventArgs) Handles gvMessages.CellFormatting

For Each row In Me.gvMessages.Rows
If row.Cells("Type").Value = "S" Then
row.Cells("Element").CellElement.BackColor = Color.Aqua
Else
End If
Next

But it did not work. Could you please give me a hint or a link for a solution.
Thnak you very much
Roberto

10 Answers, 1 is accepted

Sort by
0
Roberto Wenzel
Top achievements
Rank 2
answered on 02 Sep 2008, 03:38 PM

Hi guys,

a possible solution in VB might look like this (this solution is based on another posting in this forum):

Private Sub gvMessages_CellFormatting(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.CellFormattingEventArgs) Handles gvMessages.CellFormatting

        If TypeOf (e.CellElement.ColumnInfo) Is GridViewDataColumn AndAlso Not TypeOf (e.CellElement.RowElement) Is GridTableHeaderRowElement Then

            Dim column As GridViewDataColumn

            column = e.CellElement.ColumnInfo

            If column.FieldName = "Element" Then

                If e.CellElement.RowInfo.Cells("Type").Value = "S" Then

                    If Not e.CellElement.RowInfo.IsCurrent AndAlso Not e.CellElement.RowInfo.IsSelected Then

                        e.CellElement.BackColor = Color.LightSalmon

                        e.CellElement.TextAlignment = ContentAlignment.TopLeft

                        e.CellElement.Font = New Font(SystemFonts.DefaultFont, FontStyle.Bold)

                        e.CellElement.DrawFill = True

                    Else

                        e.CellElement.DrawFill = False

                        e.CellElement.Font = New Font(SystemFonts.DefaultFont, FontStyle.Regular)

                    End If

                ElseIf e.CellElement.RowInfo.Cells("Type").Value = "C" Then

                    If Not e.CellElement.RowInfo.IsCurrent AndAlso Not e.CellElement.RowInfo.IsSelected Then

                        e.CellElement.BackColor = Color.Khaki

                        e.CellElement.TextAlignment = ContentAlignment.TopLeft

                        e.CellElement.Font = New Font(SystemFonts.DefaultFont, FontStyle.Regular)

                        e.CellElement.DrawFill = True

                    Else

                    End If

                ElseIf e.CellElement.RowInfo.Cells("Type").Value = "E" Then

                    If Not e.CellElement.RowInfo.IsCurrent AndAlso Not e.CellElement.RowInfo.IsSelected Then

                        e.CellElement.BackColor = Color.LemonChiffon

                        e.CellElement.TextAlignment = ContentAlignment.TopRight

                        e.CellElement.Font = New Font(SystemFonts.DefaultFont, FontStyle.Regular)

                        e.CellElement.DrawFill = True

                    Else

                    End If

                End If

            End If

        End If

    End Sub

0
Jack
Telerik team
answered on 02 Sep 2008, 04:09 PM
Hi Roberto,

Thank you for contacting us.

You don't need to iterate through all rows. The current row can be obtained through the RowInfo property of the CellElement. You should also reset all properties when your condition is not true. Consider the code snippet below:

Private Sub radGridView1_CellFormatting(ByVal sender As ObjectByVal e As CellFormattingEventArgs) 
    Dim column As GridViewDataColumn = TryCast(e.CellElement.ColumnInfo, GridViewDataColumn) 
    If column IsNot Nothing AndAlso column.FieldName = "Element" Then 
        If e.CellElement.RowInfo.Cells("Type").Value.ToString() = "S" Then 
            e.CellElement.BackColor = Color.Aqua 
            e.CellElement.DrawFill = True 
        Else 
            e.CellElement.DrawFill = False 
        End If 
    End If 
End Sub 

I hope this helps. Please let me know if you need further assistance.

Kind regards,
Jack
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Roberto Wenzel
Top achievements
Rank 2
answered on 02 Sep 2008, 06:00 PM
Yes, this approach is better. It works fine. Thank you very much.
Roberto
0
george mcnitt
Top achievements
Rank 1
answered on 23 Jun 2010, 10:44 PM
Has any of this changed in Q1 2010 SP2?

I am trying to use this exact example and it keeps throwing a nullfeferenceexception on this line of code.

 

 

If e.CellElement.RowInfo.Cells("played").Value.ToString = "1" Then

 



the played column is last column in my column collection.
0
Svett
Telerik team
answered on 24 Jun 2010, 03:59 PM
Hello george mcnitt,

The exception is caused by the NULL value of GridViewCellInfo's Value property. You should check it for NULL or DBNull values before its string conversion. You can use the following code snippet as sample:
Dim value As Object = e.CellElement.RowInfo.Cells("played").Value
 
If (Not Convert.IsDBNull(value)) AndAlso value IsNot Nothing AndAlso value.ToString() = "1" Then
    ' TO DO
End If

I hope this helps.

Greetings,
Svett
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
george mcnitt
Top achievements
Rank 1
answered on 24 Jun 2010, 04:10 PM
 

That seems to have fixed the problem. Thanks.

0
Jo
Top achievements
Rank 1
answered on 11 Oct 2012, 11:05 PM
Hi,
 I am trying to format the cell value based on the value of another cell, I did exact same as you have posted.. However, I cannot get any value @ line 
"Dim value As Object = e.CellElement.RowInfo.Cells("LAB_TYPE_ID").Value "
What I want to achieve is : If the cell value of Column "TYPE" equal to 1 then format the cell value of Column "A_VALUE"  from Double to Integer (eg: 23.00 to 23), Can we do this using cell formatting?

Private Sub RadGridView1_CellFormatting(ByVal sender As Object, ByVal e As Telerik.WinControls.UI.CellFormattingEventArgs) Handles RadGridView1.CellFormatting
        Dim column As GridViewTextBoxColumn = TryCast(e.CellElement.ColumnInfo, GridViewTextBoxColumn)
        If column IsNot Nothing AndAlso column.FieldName = "A_VALUE" Then
            Dim value As Object = e.CellElement.RowInfo.Cells("TYPE").Value
            If (Not Convert.IsDBNull(value)) AndAlso value IsNot Nothing AndAlso value.ToString() = "1" Then
                e.CellElement.Value = [String].Format("{0:#}")
                e.CellElement.DrawFill = True
            End If
            e.CellElement.DrawFill = False
        End If
    End Sub

Thanks In Advance
0
Svett
Telerik team
answered on 15 Oct 2012, 12:51 PM
Hello Jo,

You can format the text depending on the value of another cell by using the CellFormatting event. In you code snippet, you should change the Text property instead of the Value property. You can use the following code snippet as sample: 
void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
    GridViewRowInfo rowInfo = e.CellElement.RowInfo;
 
    int value = Convert.ToInt32(rowInfo.Cells["ID"].Value);
 
    if (value == 2 && e.Column.Name == "Company")
    {
        e.CellElement.Text = "No Company";
    }
}

I hope this helps.

Kind regards,
Svett
the Telerik team
You’ve been asking for it and now it’s time for us to deliver. RadControls for WinForms Q3 2012 release is just around the corner. Sign up for a free webinar to see first all the latest enhancements.
0
Jo
Top achievements
Rank 1
answered on 15 Oct 2012, 03:42 PM
Hi Svett, 
  Thanks for your reply. I tried as you suggested but yet same problem.. It is not grabbing any value at line 
--- int value = Convert.ToInt32(rowInfo.Cells["ID"].Value);
The value is always nothing at this line.


Thanks
0
Svett
Telerik team
answered on 18 Oct 2012, 01:52 PM
Hi Jo Jo,

I am enclosing a sample project that demonstrates how you should use the CellFormatting event to achieve the desired scenario. If it does not fit your needs, I would kindly ask you to send a modified version where that issue occurs.

Regards,
Svett
the Telerik team
You’ve been asking for it and now it’s time for us to deliver. RadControls for WinForms Q3 2012 release is just around the corner. Sign up for a free webinar to see first all the latest enhancements.
Tags
GridView
Asked by
Roberto Wenzel
Top achievements
Rank 2
Answers by
Roberto Wenzel
Top achievements
Rank 2
Jack
Telerik team
george mcnitt
Top achievements
Rank 1
Svett
Telerik team
Jo
Top achievements
Rank 1
Share this question
or