I'm using Visual Studio 2008 (.NET 3.5 Framework, VB. NET) and I'm new using telerik rad controls.
Am I doing something wrong? Is this event the best for doing what I try to get? Thanks for the answers.
34 Answers, 1 is accepted
Welcome to the forums...
You need to use the CellFormatting event. the below example shows changing the cell back color if the cell text property = "Name" (change to suite your condition)
Let me know if you need further help
Richard
Private
Sub
RadGridView1_CellFormatting(
ByVal
sender
As
System.
Object
,
ByVal
e
As
Telerik.WinControls.UI.CellFormattingEventArgs)
Handles
RadGridView.CellFormatting
Dim
cell
As
GridDataCellElement = TryCast(e.CellElement, GridDataCellElement)
If
cell IsNot
Nothing
Then
If
cell.Text =
"Name"
Then
cell.BackColor = Color.Aqua
cell.GradientStyle = GradientStyles.Solid
cell.DrawFill =
True
Else
cell.ResetValue(LightVisualElement.DrawBorderProperty, ValueResetFlags.Local)
cell.ResetValue(LightVisualElement.BorderBoxStyleProperty, ValueResetFlags.Local)
cell.ResetValue(LightVisualElement.BorderWidthProperty, ValueResetFlags.Local)
cell.ResetValue(LightVisualElement.BorderColorProperty, ValueResetFlags.Local)
End
If
End
If
End
Sub
Thanks for your help and I apologize for my bad english.
You would still need to use the CellFormatting event because of the new virtualization system that the RadGridView uses which gives it it's good performance. For more information on virtualization and it's implications see this link
I'd suggest getting your database values outside of this event and comparing them.
As per the example, you should be using
cell.BackColor
cell.style.backcolor
The enabled property for the cell is part of the CellElement, and you can reach it from within the CellFormatting event.
hope that helps, but let me know if you have further questions.
Richard
Thanks for your answers.
No problem. A good read of the link about UI virtualization should explain it.
Please remember to mark as answer so others can quickly find the solution too.
All the best
Richard
Richard
It is always null because there you can set another type of the cell, based on some conditions, basically you can do something like this:
void
radGridView1_CreateCell(
object
sender, GridViewCreateCellEventArgs e)
{
e.CellElement =
new
CustomCellElement(e.Column, e.Row);
}
But as Richard said you can use either the CellFormatting event to color specific cells, or the RowFormatting to format rows:
A very basic example could be this:
void
radGridView1_CellFormatting(
object
sender, CellFormattingEventArgs e)
{
if
(e.CellElement.IsSelected)
{
e.CellElement.BackColor = Color.Red;
}
else
{
e.CellElement.ResetValue(LightVisualElement.BackColorProperty, Telerik.WinControls.ValueResetFlags.Local);
}
}
void
radGridView1_RowFormatting(
object
sender, RowFormattingEventArgs e)
{
if
(e.RowElement.IsSelected)
{
e.RowElement.BackColor = Color.Red;
}
else
{
e.RowElement.ResetValue(LightVisualElement.BackColorProperty, Telerik.WinControls.ValueResetFlags.Local);
}
}
Hope this helps, if you have any other questions or comments, please let me know,
Best Regards,
Emanuel Varga
Thanks to both. I'm using CellFormatting event but I'm having problems with some cells. Namely they are checkboxes and I want some of them disabled. I'm doing some tests simply asking about the value of the checkbox (if it's unchecked I try to disable it setting cell.enabled=false, it works in most of the cells but in some it isn't working; sometimes it disable cells with value true and sometimes it doesn't disable cells with value false). Another question is about method resetvalue, the compiler doesn't recognize LightVisualElement: I see is a radproperty object and I declare it but it hasn't backcolorproperty or drawborder property. I'm investigating about its use: Is it necessary for what I want to get?
Thanks for your answers.
The LightVisualElement is a member of Telerik.WinControls.UI. If you are running the latest version, then the compiler should recognise it. This is needed to be able to reset the look and fell of the cell when the condition isn't met (and as the grid uses UI virtulization and the cells are re-used, I'd say that this is required)
May I ask how you are trying to disable the cell? You may need to cast the cell to it's actual type to see a correct result, though it would be good to see your code for this event handler to confirm how you are doing this.
Thanks
Richard
Can you please post your code here, i will be more than happy to assist you more, everything will go easier and smoother like that.
Best Regards,
Emanuel Varga
Thanks to both (Richard and Emanuel) for assisting me.
This is the code: (I've made some progress but I'm still not getting the functionality I want).
Some considerations:
-Rad is an alias for Telerik.Wincontrols.UI
-primeraVez (spanish for first time) it's a boolean declared in the form. Its purpose is avoiding the code to execute every time cellformatting is handled. I only want this to be done the first time the grid is painted because I don't want the cells enabled to disable if I uncheck them. Maybe this should be done in another event? (I've tried in DataBindingComplete but I don't have access to enabled property and besides it can have some problems due to UI virtualization as Richard pointed).
Curious thing about this: If I remove the code concerning primeraVez it works well (it disables cells with checkbox value false and enables cells with checkbox value true) but as I have said before if I uncheck a cell enabled it will been disabled after editing.
-I've tried casting the cell to GridCheckBoxCellElement but I get the same result.
-I attach a picture of the form showing the grid indicating the wrong cells. (The initial values of the checkbox cells have been set in DataBindingComplete event depending on the values of a table in a database).
Thanks guys again for your help.
Private
Sub
DG1_CellFormatting(
ByVal
sender
As
Object
,
ByVal
e
As
Telerik.WinControls.UI.CellFormattingEventArgs)
Handles
DG1.CellFormatting
If
primeraVez
Then
Dim
celda
As
Rad.GridDataCellElement = TryCast(e.CellElement, Rad.GridDataCellElement)
Dim
indiceFila
As
Integer
= e.CellElement.RowIndex
Dim
indiceColumna
As
Integer
= e.CellElement.ColumnIndex
If
celda IsNot
Nothing
Then
Select
Case
celda.ColumnInfo.HeaderText.ToUpper
Case
"BIEN"
.ToUpper,
"MAL"
.ToUpper
If
celda.Value =
False
Then
celda.BackColor = Color.Red
celda.Enabled =
False
Else
celda.ResetValue(Rad.LightVisualElement.BackColorProperty, Telerik.WinControls.ValueResetFlags.Local)
'celda.BackColor = Color.White
celda.Enabled =
True
End
If
End
Select
'Last cell: I set boolean variable to false to indicate I don't want the code to be executed when grid is painted for the first time.
'I control it asking if I have reached the last cell of the grid (Maybe there is a better way).
'If I remove this verification cells enable and disable well so I think the problem is here.
If
primeraVez
And
indiceFila = DG1.Rows.Count - 1
AndAlso
indiceColumna = DG1.Columns.Count - 1
Then
primeraVez =
False
End
If
End
If
End
If
End
Sub
In my view, you won't be able to get the PrimeraVez system working as you want all the time because of the afore mentioned UI Virtualization. Because the RadGridView re-uses the cells for the data, the data in each cell changes. You will need to use the CellFormatting event, but wrap in the appropriate IF statements to you colour the cells based on the current condition or the cell, or other cells in the row.
Hope that helps
Richard
Excuse me Richard. I think I'm not undestanding well now. For what I understand should I control other conditions in the other cells of the grid? Could you post an example, please?
I'm a little unsure as to the issue you are left with. I will try to summarize:
+ Without the "PrimeraVez" boolean, all is working well, and the Checkboxes are coloured and enabled or disabled correctly
+ If you then click on one of the enabled checkboxes to uncheck it (and therefore you want to disable it), it does not change.
Is that correct?
Richard
The CellFormatting event has to fire every time, this is the behavior of the grid, so please remove that if with primeraVez and everything will work as expected.
If you want i can try to explain the virtualization mechanism of the grid. Or you can take a look at this article, it should explain everything.
Hope this helps, if you have any other questions or comments, please let me know,
Best Regards,
Emanuel Varga
+Without "PrimeraVez" boolean all cells are OK but If I uncheck a cell (enabled) this cell gets disabled, I want to keep it enabled.
+With "PrimeraVez" boolean some cells are wrong (disabled when they should be enabled and enabled when they should be disabled) but when I uncheck cells enabled they keep enabled (it's what I want).
I hope you understand me
PS: Check out the attached picture.
Hi, Emanuel.
If I remove if with primeraVez you're right: cells are drawn correctly but when I uncheck a cell it gets disabled and I want it to keep enabled.
I don't know if there were another way of achieving what I want (maybe another event) or if I could access enabled property any other way.
Thanks for your help and Richard's.
I think I understand. But as both Emanuel and I have said, you need to let the grid fire the Cellformatting event everytime.
Your issue is, is that on your CellFormatting event, you have already said that you want the any checkboxes that are unchecked to be disabled, and any checkboxes that are checked to be enabled.
I would suggest addibng another column with a value that does not change (a boolean) and tie the enabled property of the checkbox to that.
Richard
Why do you want to disable the cell?
Just set it as a different color and treat that case when updating the data source...
Best Regards,
Emanuel Varga
Please let us know if you have solved this problem if you need any more help, if you still have problems i would suggest creating a very small example and posting it here along with the exact requirements, this will allow us to help you solve your problems faster and possibly better.
Best Regards,
Emanuel Varga
Hi, Emanuel. Initially I tried to set the cells with another color in DataBindingComplete event but it doesn't work. I do it casting the cell to RadGridViewCellInfo and setting cell.style.backcolor but it doesn't work (I've seen there are four backcolor properties, why?).
It doesn't work in cellformatting event setting backcolor even removing the if with primeraVez.
Thanks again for your assistance.
To change the back colour of a cell you need to use the Cellformatting event. Please try the very first exmaple that I gave: I.e.
Private
Sub
RadGridView1_CellFormatting(
ByVal
sender
As
System.
Object
,
ByVal
e
As
Telerik.WinControls.UI.CellFormattingEventArgs)
Handles
RadGridView.CellFormatting
Dim
cell
As
GridDataCellElement = TryCast(e.CellElement, GridDataCellElement)
If
cell IsNot
Nothing
Then
If
cell.Text =
"Name"
Then
cell.BackColor = Color.Aqua
cell.GradientStyle = GradientStyles.Solid
cell.DrawFill =
True
Else
cell.ResetValue(LightVisualElement.DrawBorderProperty, ValueResetFlags.Local)
cell.ResetValue(LightVisualElement.BorderBoxStyleProperty, ValueResetFlags.Local)
cell.ResetValue(LightVisualElement.BorderWidthProperty, ValueResetFlags.Local)
cell.ResetValue(LightVisualElement.BorderColorProperty, ValueResetFlags.Local)
End
If
End
If
End
Sub
the reason that there is more than one back color is that you can apply a gradient to blend in color from one to another.
Please try the above example so we can continue to hel solve your issues
Thanks
Richard
Maybe I should try the aproach you told some posts ago: "Adding another column with a value that does not change (a boolean) and tie the enabled property of the checkbox to that." (I would need two extra columns in that case I think one for each boolean column I want to control).
Anyways, thanks again for your effort and assistance.
Don't create another column, just use the Tag property of the cell to store a value there, and read it in the CellFormatting event, and based on that value decide what to do.
Hope this helps, if you have any other questions or comments, please let me know,
Best Regards,
Emanuel Varga
That's the idea. if you want to maintain the color based on the initial values, it would be best to have an additional column to store these (that are not visible to the user) and base your color on those.
This way, you will not encounter the issue of the checkbox changing.
Hope this helps. If you have any other questions, just let me know, or if you find that this has worked for you, please remember to mark as answer so others can find the solution too.
All the best
Richard
Both approaches (property tag and extra hidden column in datatable) tested and both worked.
Thanks to both for your effort and assistance. :)
PS: Is there anyway to mark both answers or only one of them can be marked?
You can mark / should mark all of the answers that are / were helpful in saving your problem.
Glad to be able to help,
If you have any more questions please just let me know,
Best Regards,
Emanuel Varga
Hi. I've tested this again and It seems I didn't test it well. Richard's approach works (extra columns, eventually I'm using this) but Emanuel's approach doesn't work well. The color of the cell is changing. The reason is that the tag property doesn't maintain its value between DataBindingComplete event (I set its value for the first time (true or false)) and CellFormatting (I read its value and yields nothing in every case).
Thanks for your answer guys.
Because in the DataBindingComplete you are setting the Tag value for the CellInfo and in the CellFormatting you are reading it for the CellElement, please take a look at the following code:
void
radGridView1_CellFormatting(
object
sender, CellFormattingEventArgs e)
{
if
(e.CellElement.RowInfo.Cells[0].Tag !=
null
&& e.CellElement.RowInfo.Cells[0].Tag.Equals(
true
))
{
e.CellElement.BackColor = Color.Red;
e.CellElement.DrawFill =
true
;
}
else
{
e.CellElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
}
}
void
radGridView1_DataBindingComplete(
object
sender, GridViewBindingCompleteEventArgs e)
{
foreach
(var row
in
radGridView1.Rows)
{
if
(row.Index % 2 == 0)
{
row.Cells[0].Tag =
true
;
}
}
}
Hope this helps, if you have any other questions or comments, please let me know,
Best Regards,
Emanuel Varga
Telerik WinForms MVP
e.cellElement.rowInfo(e.cellElement.columnIndex).Tag (If I'm mistaken someone to correct me but it's the same cell (OK?) so it should have the same value in Tag property) gives the value I'm expecting (true or false).
Thanks again.
It was my fault because i should have attached an example but lately I've been extremely busy (sorry again).
The thing is this, the CellElement is not the same thing as the CellInfo, please take a look at this article, or this more detailed article for further clarifications about the GridVirtualization engine.
Hope this helps, if you have any other questions or comments, please let me know,
Best Regards,
Emanuel Varga
Telerik WinForms MVP
Thanks again.
Thank you for writing.
Yes, the event is fired several times. However, if you are only setting the cell styles in it there should not be any performance impact. Could you please share the code that you are using in this event? Are you performing any time-consuming operations in it?
I am looking forward to your reply.
Regards,
Dimitar
Telerik