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
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
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 Object, ByVal 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.
Roberto
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.
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
That seems to have fixed the problem. Thanks.
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
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
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
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