This is a migrated thread and some comments may be shown as answers.

CreateCell event cellelement is always null

34 Answers 416 Views
GridView
This is a migrated thread and some comments may be shown as answers.
LLC
Top achievements
Rank 1
LLC asked on 04 Nov 2010, 05:42 PM
Hi, this is my first post. I'm using RadGridView and I want some cells have different colour if a condition is fulfilled or some cells disabled if the same condition or another is fulfilled. I am handling CreateCell event but the problem is e.cellelement is always nothing and I get exception. I'm using RadControls for Windows 2010 Q2 SP2 trial.
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

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 04 Nov 2010, 09:27 PM
Hi LLC,

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
0
LLC
Top achievements
Rank 1
answered on 05 Nov 2010, 12:22 PM
Thanks, Richard. It's a little more complicated. I would only want to do the process only once because it doesn't depend on the values in the cells, it depens on the values I have in another table of a database (Let's say it's the structure) and these values correspond to the values I have in the grid in position (I hope you have understood me). If for example I sort the grid the values don't correspond and it makes the process again. If I'm not mistaken Cellformating fires each time the grid is modified. I was trying to do this process in DataBindingComplete event but it seems cell.style.backcolor doesn't work (it doesn't change the colour). Another question: Could I access enabled property in this event?, I don't find it. I would need it in order to disable some checkboxes which don't fulfil the condition.
 
Thanks for your help and I apologize for my bad english.
0
Richard Slade
Top achievements
Rank 2
answered on 05 Nov 2010, 12:32 PM
Hi LLC, 

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
Not
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
0
LLC
Top achievements
Rank 1
answered on 05 Nov 2010, 01:04 PM
Thanks Richard. I'll investigate about the new virtualization system. Anyways I think cell.style.backcolor doen't work in DataBindingComplete event (for example cell.style.forecolor it works. I've checked it out).  And I'd like to know if it's normal that e.cellelement in CreateCell event is always nothing (maybe the value doesn't still exist because it's about to create the cell?)

Thanks for your answers.
0
Richard Slade
Top achievements
Rank 2
answered on 05 Nov 2010, 01:24 PM
Hi LLC, 

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
0
Richard Slade
Top achievements
Rank 2
answered on 05 Nov 2010, 01:49 PM
P.S - Yes, in your scenario, e.CellElement will always be nothing in CreateCell
Richard
0
Emanuel Varga
Top achievements
Rank 1
answered on 05 Nov 2010, 02:04 PM
Hello,

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);
}
Where you can say that this cell should be a custom cell element.

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
0
LLC
Top achievements
Rank 1
answered on 05 Nov 2010, 05:56 PM

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.

0
Richard Slade
Top achievements
Rank 2
answered on 05 Nov 2010, 06:15 PM
Hi, 

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

0
Emanuel Varga
Top achievements
Rank 1
answered on 05 Nov 2010, 10:39 PM
Hello again LLC,

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
0
LLC
Top achievements
Rank 1
answered on 08 Nov 2010, 12:24 PM

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
0
Richard Slade
Top achievements
Rank 2
answered on 08 Nov 2010, 12:41 PM
Hola LLC,

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
0
LLC
Top achievements
Rank 1
answered on 08 Nov 2010, 01:09 PM
"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."
 
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?
0
Richard Slade
Top achievements
Rank 2
answered on 08 Nov 2010, 01:19 PM
Hi LLC,

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
0
Emanuel Varga
Top achievements
Rank 1
answered on 08 Nov 2010, 01:22 PM
Hello LLC,

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
0
LLC
Top achievements
Rank 1
answered on 08 Nov 2010, 01:35 PM
Hi, Richard. It's this way:

+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.
0
LLC
Top achievements
Rank 1
answered on 08 Nov 2010, 01:49 PM

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.

0
Richard Slade
Top achievements
Rank 2
answered on 08 Nov 2010, 01:52 PM
Hi LLC,

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
0
Emanuel Varga
Top achievements
Rank 1
answered on 08 Nov 2010, 01:55 PM
Hello again,

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
0
Emanuel Varga
Top achievements
Rank 1
answered on 08 Nov 2010, 02:48 PM
Hello again LLC,

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
0
LLC
Top achievements
Rank 1
answered on 08 Nov 2010, 02:56 PM

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.

0
Richard Slade
Top achievements
Rank 2
answered on 08 Nov 2010, 03:01 PM
Hi LLC,

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
0
LLC
Top achievements
Rank 1
answered on 08 Nov 2010, 05:46 PM
Hi, Richard. Using your example setting the backcolor is working (By the way in DataBindingComplete it doesn't never work, however forecolor it works).  But I still have the same problem, if I uncheck the cell the color changes and I don't want that (I'd like it always stayed the same color if initially was checkes though after I uncheck it). I've set readonly to false in the cells I don't want to be modified in DataBindingComplete event. This way cells initially unchecked can't be modified and they have the backcolor I want but as I have told you if I uncheck a cell initially checked (its readonly property is false) the backcolor changes and I don't want that. Is there any way to access readonly property in CellFormatting event?  If I could verify its value I think I could solve this definitely.

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.

0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 08 Nov 2010, 05:49 PM
Hello LLC,

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
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 08 Nov 2010, 05:50 PM
Hi LLC, 

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
0
LLC
Top achievements
Rank 1
answered on 10 Nov 2010, 09:35 AM
Hi, Emanuel, 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?
0
Emanuel Varga
Top achievements
Rank 1
answered on 10 Nov 2010, 09:49 AM
Hello again,

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
0
LLC
Top achievements
Rank 1
answered on 16 Nov 2010, 10:40 AM

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.

 

0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 16 Nov 2010, 10:52 AM
Hello LLC,

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
0
LLC
Top achievements
Rank 1
answered on 16 Nov 2010, 12:33 PM
Thanks Emanuel. This time it's working 100% sure. The problem is (or maybe is a misunderstanding mine) if you read in CellFormating e.cellelement.tag is nothing but however if you read
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.
0
Emanuel Varga
Top achievements
Rank 1
answered on 16 Nov 2010, 12:40 PM
Hello again LLC,

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
0
LLC
Top achievements
Rank 1
answered on 16 Nov 2010, 12:46 PM
Thanks Emanuel. No problem. So they aren't the same object, that explains it. I didn't read enough well the articles the first time you and Richard told me about them. After this fight I understand better radgridview virtualization and its implications.

Thanks again.
0
Bashir
Top achievements
Rank 1
answered on 14 Nov 2015, 09:36 AM
if i use the cellformating, gidview be very slow,becouse this even raise more than on or two number.
0
Dimitar
Telerik team
answered on 16 Nov 2015, 12:56 PM
Hello Bashir,

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
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 Feedback Portal and vote to affect the priority of the items
Tags
GridView
Asked by
LLC
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
LLC
Top achievements
Rank 1
Emanuel Varga
Top achievements
Rank 1
Bashir
Top achievements
Rank 1
Dimitar
Telerik team
Share this question
or