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

RadGridView loses format when it is updated.

6 Answers 169 Views
GridView
This is a migrated thread and some comments may be shown as answers.
LLC
Top achievements
Rank 1
LLC asked on 18 Nov 2010, 04:49 PM
Hi, in my program I have a RadGridView. Some of the cells are formatted with different backgroundcolor or they are disabled. The first time I bind the grid with data read from a table of a database it shows correct but If I update the grid it loses the format (All the cells are white and enabled) . 

The cells are formatted according to this approach  http://www.telerik.com/community/forums/winforms/gridview/createcell-event-cellelement-is-always-null.aspx (First it sets a tag for each cell in DataBindingComplete and subsequently depending on this value it sets backcolor and enabled properties for the cell.)

Here is the code (Cheers to Emanuel Varga for it):

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;
        }
    }
}

Thanks for the answers.

6 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 18 Nov 2010, 04:58 PM
Hello LLC,

i think this may be the same as a bug I have just reported.

Can you just try calling
e.CellElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local); 
        e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
as the first thing you do in your CellFormatting event please, and then perform the rest as you are doing there and let me know the result.

Thanks
Richard

0
LLC
Top achievements
Rank 1
answered on 18 Nov 2010, 05:33 PM

Hi, Richard. Unluckily it doesn't work :(

 

 

0
Richard Slade
Top achievements
Rank 2
answered on 18 Nov 2010, 05:43 PM
Sorry to hear that. If you breakpoint one.CellElement.BackColor is it ever reaching that code?
Richard
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 18 Nov 2010, 10:30 PM
Hello LLC,

Apologies this is in VB for now, but I've tried binding to a simple datasource, with a button on the form that re-binds to a new datasource, using your CellFormatting code and your DataBindingComplete method and I couldn't replicate your issue.

Please can you try it also and let me know if it runs ok on yours?

Imports Telerik.WinControls
Imports Telerik.WinControls.UI
Imports System.Globalization
  
Public Class Form1
  
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  
        Dim list As New List(Of person)
        list.Add(New person("Richard", 1))
        list.Add(New person("Bob", 2))
        list.Add(New person("Stewart", 3))
        list.Add(New person("Chris", 4))
        list.Add(New person("Leisl", 1))
        list.Add(New person("Tom", 5))
        list.Add(New person("Oly", 6))
  
        Me.RadGridView1.DataSource = list
  
  
    End Sub
  
    Private Sub RadButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadButton1.Click
        Dim list As New List(Of person)
        list.Add(New person("Ian", 1))
        list.Add(New person("Bert", 2))
        list.Add(New person("Brian", 3))
        list.Add(New person("Ethel", 4))
        list.Add(New person("Mabel", 1))
        list.Add(New person("Gertrude", 5))
        list.Add(New person("Molly", 6))
  
        Me.RadGridView1.DataSource = list
    End Sub
  
    Private Sub RadGridView1_CellFormatting(ByVal sender As System.Object, _
        ByVal e As Telerik.WinControls.UI.CellFormattingEventArgs) Handles RadGridView1.CellFormatting
  
        If e.CellElement.RowInfo.Cells(0).Tag IsNot Nothing AndAlso e.CellElement.RowInfo.Cells(0).Tag.Equals(True) Then
            e.CellElement.BackColor = Color.Red
            e.CellElement.DrawFill = True
        Else
            e.CellElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local)
            e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local)
        End If
  
    End Sub
  
    Private Sub RadGridView1_DataBindingComplete(ByVal sender As System.Object, ByVal e As Telerik.WinControls.UI.GridViewBindingCompleteEventArgs) Handles RadGridView1.DataBindingComplete
        For Each row As GridViewRowInfo In RadGridView1.Rows
            If row.Index Mod 2 = 0 Then
                row.Cells(0).Tag = True
            End If
        Next
    End Sub
  
  
End Class
  
Public Class person
    Private m_Id As Integer
    Private m_Name As String
  
    Public Sub New()
    End Sub
  
    Public Sub New(ByVal name As String, ByVal id As Integer)
        m_Name = name
        m_Id = id
    End Sub
  
    Public Property Name() As String
        Get
            Return m_Name
        End Get
        Set(ByVal value As String)
            m_Name = value
        End Set
    End Property
  
    Public Property Id() As Integer
        Get
            Return m_Id
        End Get
        Set(ByVal value As Integer)
            m_Id = value
        End Set
    End Property
End Class

Let me know if I can help further
Richard
0
LLC
Top achievements
Rank 1
answered on 19 Nov 2010, 10:16 AM
Hi, Richard. Don't worry about if the code is VB or C#. I've tested your sample code and it works (maybe due to the data are in a list?) but in my case it keeps failing (My data come from a datatable , the database is SQL SERVER 2005). The difference between your code and mine one is that after updating the table I wasn't setting datasource property of the grid to the table; I've set it  but it fails too). For the moment to sort it out I set datasource property of RadGridView to nothing and then I set it to the table which contains the data again. This way the cells are well formatted.

Thanks for your effort and assistance.
0
Richard Slade
Top achievements
Rank 2
answered on 19 Nov 2010, 10:19 AM
Hi LLC,

I'm glad you solved your issue. If you feel any of the suggestions helped, please mark as answer. And if there's anything else you need just let me know.

Richard
Tags
GridView
Asked by
LLC
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
LLC
Top achievements
Rank 1
Share this question
or