Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WinForms > GridView > Conditionally format cell value based on value of another cell

Not answered Conditionally format cell value based on value of another cell

Feed from this thread
  • Posted on Sep 1, 2008 (permalink)

    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

    Reply

  • Posted on Sep 2, 2008 (permalink)

    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

    Reply

  • Say Hello to Telerik's PivotGrid for ASP.NET AJAX, Silverlight, WPF and WinForms. Now packed with OLAP support.
  • Jack Jack admin's avatar

    Posted on Sep 2, 2008 (permalink)

    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.

    Reply

  • Posted on Sep 2, 2008 (permalink)

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

    Reply

  • Posted on Jun 23, 2010 (permalink)

    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.

    Reply

  • Svett Svett admin's avatar

    Posted on Jun 24, 2010 (permalink)

    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

    Reply

  • Posted on Jun 24, 2010 (permalink)

     

    That seems to have fixed the problem. Thanks.

    Reply

  • Jo avatar

    Posted on Oct 11, 2012 (permalink)

    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

    Reply

  • Svett Svett admin's avatar

    Posted on Oct 15, 2012 (permalink)

    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.

    Reply

  • Jo avatar

    Posted on Oct 15, 2012 (permalink)

    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

    Reply

  • Svett Svett admin's avatar

    Posted on Oct 18, 2012 (permalink)

    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.

    Reply

  • Say Hello to Telerik's PivotGrid for ASP.NET AJAX, Silverlight, WPF and WinForms. Now packed with OLAP support.

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WinForms > GridView > Conditionally format cell value based on value of another cell
Related resources for "Conditionally format cell value based on value of another cell"

[ Features | Demos | Documentation | Knowledge Base | Telerik TV | Code Library | Step-by-step Tutorial | Blogs | Self-Paced Trainer ]