Conditionally format cell value based on value of another cell

11 posts, 0 answers
  1. Roberto Wenzel
    Roberto Wenzel avatar
    37 posts
    Member since:
    Sep 2012

    Posted 01 Sep 2008 Link to this post

    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

  2. Roberto Wenzel
    Roberto Wenzel avatar
    37 posts
    Member since:
    Sep 2012

    Posted 02 Sep 2008 Link to this post

    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

  3. Jack
    Admin
    Jack avatar
    2209 posts
    Member since:
    Sep 2012

    Posted 02 Sep 2008 Link to this post

    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.

  4. Roberto Wenzel
    Roberto Wenzel avatar
    37 posts
    Member since:
    Sep 2012

    Posted 02 Sep 2008 Link to this post

    Yes, this approach is better. It works fine. Thank you very much.
    Roberto

  5. george mcnitt
    george mcnitt avatar
    14 posts
    Member since:
    May 2010

    Posted 23 Jun 2010 Link to this post

    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.

  6. Svett
    Admin
    Svett avatar
    728 posts
    Member since:
    Oct 2014

    Posted 24 Jun 2010 Link to this post

    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

  7. george mcnitt
    george mcnitt avatar
    14 posts
    Member since:
    May 2010

    Posted 24 Jun 2010 Link to this post

     

    That seems to have fixed the problem. Thanks.

  8. Jo
    Jo avatar
    5 posts
    Member since:
    Jul 2012

    Posted 11 Oct 2012 Link to this post

    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

  9. Svett
    Admin
    Svett avatar
    728 posts
    Member since:
    Oct 2014

    Posted 15 Oct 2012 Link to this post

    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.

  10. Jo
    Jo avatar
    5 posts
    Member since:
    Jul 2012

    Posted 15 Oct 2012 Link to this post

    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

  11. Svett
    Admin
    Svett avatar
    728 posts
    Member since:
    Oct 2014

    Posted 18 Oct 2012 Link to this post

    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.

Back to Top