Hi
I have the following code ..
Private
Sub
RadGridView1_CellFormatting (sender
As
Object
, e
As
UI.CellFormattingEventArgs)
Handles
RadGridView1.CellFormatting
If
e.CellElement.ColumnInfo.Name =
"Document"
Then
e.CellElement.ForeColor = Color.Red
Else
e.CellElement.ResetValue (LightVisualElement.ForeColorProperty, ValueResetFlags.Local)
End
if
If
e.CellElement.ColumnInfo.Name =
"Document"
Then
If
e.CellElement.RowInfo.Cells (
"Document"
). Value> = RadTextBox4.Text
And
e.CellElement.RowInfo.Cells (
"Document"
). Value <= RadTextBox5.Text
Then
e.CellElement.DrawFill =
True
e.CellElement.BackColor = Color.Yellow
e.CellElement.ForeColor = Color.Blue
e.CellElement.GradientStyle = Telerik.WinControls.GradientStyles.Radial
e.CellElement.Font =
New
Font (
"Book Antiqua"
, 12.75 !, FontStyle.Bold)
End
if
End
if
End
Sub
works perfectly !
Now I would like to add only the sum in the
"Document"
column that only e.CellElement.BackColor = Color.Yellow or also e.CellElement.GradientStyle = Telerik.WinControls.GradientStyles.Radial have.
I would like to have this sum displayed in radlabel3.text, how do I get it there?
8 Answers, 1 is accepted
Hello Andi,
Thank you for the provided code snippet.
According to it, I suppose that the "Document" column contains numeric values and you color some rows in RadGridView based on some conditions. If I understand you correctly you would like to get the sum of rows that are colored in yellow and have GradientStyles.Radial. To do so, I can suggest to iterate the cells outside the formatting event and use the same condition that you use in the CellFormatting event to calculate the sum.
Note, since RadGridView uses UI virtualization for its cells the CellFormatting event should be fired many times and it should be used for formatting purposes only. This is why if you want to make some calculations it is suitable to do it outside this event, for example in a separate method:
Class SurroundingClass
Public Sub New()
InitializeComponent()
CalculateSum()
End Sub
Private Sub CalculateSum()
For Each rowInfo In Me.radGridView1.Rows
For Each cellInfo As GridViewCellInfo In rowInfo.Cells
If cellInfo.ColumnInfo.Name = "Document" Then
If CDec(cellInfo.Value) >= 50 AndAlso CDec(cellInfo.Value) <= 100 Then
sum += CDec(cellInfo.Value)
End If
End If
Next
Next
Console.WriteLine(sum)
Me.radLabel1.Text = sum.ToString()
End Sub
End Class
I would like to note that RadGridView offers summary rows which may be suitable for your scenario. If you don't want to sum all the cell values, you can construct a custom summary item and override its Evaluate method where you can implement the desired logic for the calculation. Additional information is available in the following help article: https://docs.telerik.com/devtools/winforms/controls/gridview/rows/summary-rows
I hope this information helps. If you have further questions please let me know.
Regards,
Nadya
Progress Telerik
Our thoughts here at Progress are with those affected by the outbreak.
Super Thank you it works Perfect..
Have always worked with Visual Studio "Datagrid" in the past
but I have to say the Telerik RadGridview is something of super,
these many possibilities it has simply super.
I'm still a beginner with all the Telerik components to find myself there
because there are many differences from Visual Studio "Datagrid".
But great it's such a forum as you give the one help, my English is
also not so good sorry, so again
Thank you very much
Andi
Hello Andi,
I am really glad that the suggested solution works for you. Thank you for sharing your feedback about the experience you have with RadGridView control. We really appreciate it.
Do not hesitate to contact us in case of any further difficulties.
Regards,
Nadya
Progress Telerik
Our thoughts here at Progress are with those affected by the outbreak.
Hi Nadya,
have a problem with the RadTextBox4.Text and RadTextBox5.Text, these should only numbers and commas
as input allow, example: "23,40 " .
Therefore, include the following code:
Private Sub RadTextBox4_KeyPress(sender As Object, e As KeyPressEventArgs) Handles RadTextBox4.KeyPress
'* This function only allows numeric and comma inputs!!
Const Numbers" = "0123456789, "
If Asc(e.KeyChar) <> 8 Then
If InStr(Numbers, Chr(Asc(e.KeyChar))) = 0 Then
e.Handled = True
Exit Sub
End If
End If
End Sub
Private Sub RadTextBox5_KeyPress(sender As Object, e As KeyPressEventArgs) Handles RadTextBox5.KeyPress
'* This function only allows numeric input!!
Const Numbers" = "0123456789, "
If Asc(e.KeyChar) <> 8 Then
If InStr(Numbers, Chr(Asc(e.KeyChar))) = 0 Then
e.Handled = True
Exit Sub
End If
End If
End Sub
Also works well, only I noticed that if the user accidentally after the two
comma enters another comma as here "23,40", then the program crashes off with
the following error code:
-System.InvalidCastException: "Invalid conversion from the string 23.40, to type Double."-
on this line "*"
For Each rowInfo In Me.RadGridView1.Rows
For each cellInfo as GridViewCellInfo in rowInfo.cells
If cellInfo.ColumnInfo.Name = "Document" Then
** --> If CDec(cellInfo.Value) >= RadTextBox4.Text And CDec(cellInfo.Value)
sum += CDec(cellInfo.Value)
End If
End If
Next
Next
Question:
Can I somehow intercept this or prevent it with another KeyPress event?
Hi, Andi,
Following the provided information it seems that you would like to enter only numeric values in the text box and later use them in the grid. If you do so, I can suggest to consider using RadMaskedEditBox. RadMaskedEditBox is a themeable text box that formats and constrains text to a predefined pattern or a pattern you define. The MaskType property defines what type of mask would be used in the masked box.
In your case when you want to display decimal values you can set the MaskType to Numeric. I hope you will find it more useful rather than restricting RadTextBox from undesired user input. Thus, the user will be able to type only numeric values without extra commas or other symbols. Please refer to the following articles:
https://docs.telerik.com/devtools/winforms/controls/editors/maskededitbox/maskededitbox
https://docs.telerik.com/devtools/winforms/controls/editors/maskededitbox/numeric-masks
Could you give RadMaskedEditBox a try and let me know if it is suitable for you.
I am looking forward to your reply.
Regards, Author nickname Progress Telerik
Our thoughts here at Progress are with those affected by the outbreak.
Hi Nadya,
that with the RadMaskedEditBox was a good idea as it does not allow two commas,
Works super now.
At the beginning there was only a small error because the RadMaskedEditBox when starting the application
the field is empty and I got an error message :
- System.InvalidCastException: "Invalid conversion from string to type Double. -
First tried to enter this in the Form_Load erreigniss:
Me.RadMaskedEditBox1.MaskType = Telerik.WinControls.UI.MaskType.Numeric
Me.RadMaskedEditBox1.Mask = "C"
thought that when loading the shape the Me.RadMaskedEditBox.Mask = "C" = "0.00 € " would appear
had unfortunately also not worked,field also remained empty, same error message.
Then have tried it in the RadGridView1_CellFormatting with:
Me.RadMaskedEditBox1.MaskType = Telerik.WinControls.UI.MaskType.Numeric
Me.RadMaskedEditBox1.Mask = "C"
there it has then the Me.RadMaskedEditBox.Mask = "C" at the start of the application
in the "0.00 € " field,no more error message.
So the field must not be empty when starting the application otherwise there is this error.
Many people have a different idea of how to prevent this.
But thanks again for the hint with RadMaskedEditBox..
Hi Nadya,
Sorry was a little too hasty, it got done.
Have entered it in the properties of the RadMaskedEditBox :
Mask Type = Numeric
Mask = C
All Super Thanks Andi
Hi, Andi,
I am really glad that the suggested RadMaskedEditBox control is suitable for your scenario.
Should you have further questions I will be glad to help.
Regards,
Nadya
Progress Telerik
Our thoughts here at Progress are with those affected by the outbreak.