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

RadGridView not Refreshing after binding data

14 Answers 1536 Views
GridView
This is a migrated thread and some comments may be shown as answers.
SachinC
Top achievements
Rank 1
SachinC asked on 04 Oct 2010, 02:00 PM
Hello,

i am having frmHome.vb with radgridview control, i am opening frmHome.vb form in RadDoc tab in frmMainapplication.vb form.
data is binded to gridview, in grid view there is on commanbutton column. by clicking the command button i am opening frmTagDetails.vb in tab. in formDetails i am having one button. if i click on that button a new form is opend to with two radion buttons.
after selectiecion one of the option from it and clicking on OK button. and record is inserted in database table. testUser.
after inserting record in testuser i need to update the radgridview. automatically and new user detals must be displayed. my
frmhome.vb form is already open.

Dim objfrmHome As New frmHome
  
objfrmHome.gridViewHome.DataSource = Nothing
  
AssetBusinessLogic.GetDataToFillGridView(_assetChecklist.Asset.Project.ProjectID)
  
objfrmHome.gridViewHome.MasterTemplate.DataSource = AssetBusinessLogic.aTLi
  
objfrmHome.gridViewHome.Refresh()
  
objfrmHome.gridViewHome.MasterTemplate.Refresh()
  
objfrmHome.gridViewHome.MasterTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill
  
i am using this code but the while loading the details from database i am getting the lattest details.
but after binding to grid the grid is not refreshed.

i have stucked over here.

Thanks in advance

14 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 04 Oct 2010, 02:14 PM
Hello Sachin,

You are setting here the DataSource for the Grid to nothing, and after you are getting new data, you are setting the DataSource for the MasterTemplate to your new data?

Try setting it back to the grid and see if it's OK, like:
Dim objfrmHome As New frmHome
   
objfrmHome.gridViewHome.DataSource = Nothing
   
AssetBusinessLogic.GetDataToFillGridView(_assetChecklist.Asset.Project.ProjectID)
   
objfrmHome.gridViewHome.DataSource = AssetBusinessLogic.aTLi
 
objfrmHome.gridViewHome.MasterTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill

And one more thing you should consider, if you want to do some operations on the bounded grid, like BestFitColumns(), or hiding some columns, or just for validation that the grid has been bounded, you should implement the DataBindingComplete event and handle that logic there.

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
0
SachinC
Top achievements
Rank 1
answered on 04 Oct 2010, 02:52 PM
Hi Emanuel ,

Thanks for your reply!

but still its not working.

the same code is used in frmHome.vb to bind the grid.

AssetBusinessLogic.GetDataToFillGridView(_assetChecklist.Asset.Project.ProjectID) 
    
objfrmHome.gridViewHome.MasterTemplate.DataSource = AssetBusinessLogic.aTLi

i am calling the below posted code from another form to update the grid automatically.
in grid i have added some columns manually.

if i open the frmhome in seprate tab, the updated details are displayed.
Dim objfrmHome As New frmHome()
Dim fMain = TryCast(Me.Owner.ParentForm, frmMainApplication)
fMain.AddDocumentTab(objfrmHome)

Please suggest.

Thanks
0
Emanuel Varga
Top achievements
Rank 1
answered on 04 Oct 2010, 03:01 PM
Hello Sachin,

A few question,
1. Is the newly received data coming from the main thread or from a different thread (this is very important)?
2. Please try without setting the previous DataSource to Nothing

If you'll still have problems i will create an example on which we can test to find the problem.

Best Regards,
Emanuel Varga
0
SachinC
Top achievements
Rank 1
answered on 07 Oct 2010, 10:01 AM
Hi Emanuel ,

Thanks for your reply!

you can send me an example for updating/refreshing radgridview.

i am using Linq, entity framework and vb.net

Thanks!
0
Emanuel Varga
Top achievements
Rank 1
answered on 07 Oct 2010, 10:20 AM
Hello Sachin,

1. Is one with business objects OK?
2. Is it on in C# or should i convert it to vb?
3. Event for data update Main thread or background thread?

Please answer these questions and i will prepare an example.

Best Regards,
Emanuel Varga
0
Emanuel Varga
Top achievements
Rank 1
answered on 07 Oct 2010, 10:45 AM
Hello Sachin,

Did you try just calling a Refresh() on the grid?

Best Regards,
Emanuel Varga
0
Julian Benkov
Telerik team
answered on 08 Oct 2010, 07:52 AM
Hi Sachin,

Please check whether your DataSource object (in you case "AssetBusinessLogic.aTLi") contains some data before the binding operation. The Refresh operation is not needed, the only needed operation in this scenario is assigning the new DataSource with data.

I hope this is helpful.

Kind regards,
Julian Benkov
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
0
SachinC
Top achievements
Rank 1
answered on 14 Oct 2010, 10:41 AM
Hello Emanuel ,
I have used Refresh() as well

objfrmHome.gridViewHome.MasterTemplate.DataSource = Nothing
            AssetBusinessLogic.GetDataToFillGridView(_assetChecklist.Asset.Project.ProjectID)
            objfrmHome.gridViewHome.MasterTemplate.DataSource = AssetBusinessLogic.aTLi
            objfrmHome.gridViewHome.Refresh()
            objfrmHome.gridViewHome.MasterTemplate.Refresh()
            objfrmHome.gridViewHome.MasterTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill


but no success.
Answeres for your Questions:

1. Is one with business objects OK?  - OK
2. Is it on in C# or should i convert it to vb? - it will be great pleasure if you convert it into VB
3. Event for data update Main thread or background thread? Backgroun tread



Hello Julian Benkov ,

if you see my above code snippet
i am reassiging the datasource. and the datasource object (AssetBusinessLogic.aTLi) contains updated details.


Thanks
0
Richard Slade
Top achievements
Rank 2
answered on 14 Oct 2010, 10:55 AM
Hi Sachin, 

As described you shouldn't need the refresh and should check your data source or if you are doing anything else that might affect the grid on other events. The simple sample below has a RadGridView and a RadButton on a Form. On load of the form the grid is populate with simple object based data and on click of the button the RadGridView is refreshed with a new data source. Im using the latest 2010 914 release. 
Hope that helps
Richard
Public Class Form1
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim somethingList As New System.Collections.Generic.List(Of Something)
        somethingList.Add(New Something("My thing", 1))
        somethingList.Add(New Something("your thing", 2))
        somethingList.Add(New Something("his thing", 3))
        Me.RadGridView1.DataSource = somethingList
    End Sub
 
    Private Sub RadButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadButton1.Click
        Dim somethingList As New System.Collections.Generic.List(Of Something)
        somethingList.Add(New Something("Her thing", 4))
        somethingList.Add(New Something("bobs thing", 5))
        somethingList.Add(New Something("Brians thing", 6))
        Me.RadGridView1.DataSource = somethingList
    End Sub
End Class
 
Public Class Something
    Private m_Id As Integer
    Private m_Name As String
 
    Public Sub New(ByVal name As String, ByVal id As Integer)
        m_Name = name
        m_Id = id
    End Sub
 
    Public ReadOnly Property Name() As String
        Get
            Return m_Name
        End Get
    End Property
 
    Public ReadOnly Property Id() As Integer
        Get
            Return m_Id
        End Get
    End Property
End Class
0
Richard Slade
Top achievements
Rank 2
answered on 14 Oct 2010, 10:57 AM
Ah, I missed it that you are using a background thread. I will prepare you a sample with a background worker
0
Richard Slade
Top achievements
Rank 2
answered on 14 Oct 2010, 11:00 AM
There you go. Let me know if this works for you. 

Public Class Form1
 
    Private m_DataSource As System.Collections.Generic.List(Of Something)
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        m_DataSource = New System.Collections.Generic.List(Of Something)
        m_DataSource.Add(New Something("My thing", 1))
        m_DataSource.Add(New Something("your thing", 2))
        m_DataSource.Add(New Something("his thing", 3))
        Me.RadGridView1.DataSource = m_DataSource
    End Sub
 
    Private Sub RadButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadButton1.Click
        Me.BackgroundWorker1.RunWorkerAsync()
    End Sub
 
    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        m_DataSource = New System.Collections.Generic.List(Of Something)
        m_DataSource.Add(New Something("Her thing", 4))
        m_DataSource.Add(New Something("bobs thing", 5))
        m_DataSource.Add(New Something("Brians thing", 6))
    End Sub
 
    Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        Me.RadGridView1.DataSource = m_DataSource
    End Sub
End Class
 
Public Class Something
    Private m_Id As Integer
    Private m_Name As String
 
    Public Sub New(ByVal name As String, ByVal id As Integer)
        m_Name = name
        m_Id = id
    End Sub
 
    Public ReadOnly Property Name() As String
        Get
            Return m_Name
        End Get
    End Property
 
    Public ReadOnly Property Id() As Integer
        Get
            Return m_Id
        End Get
    End Property
End Class
0
Emanuel Varga
Top achievements
Rank 1
answered on 14 Oct 2010, 11:23 AM
Hello guys,

Sorry for the late answer, but I have something to add to Richard's answer

The first thing, you have to remove the line :
Me
.RadGridView1.DataSource = somethingList
from inside BackGroundWorker_DoWork method, because this will cause an exception accessing the control from a different thread.
The second thing, to better resemble your case should be opening a form on button click, something like:
Private Sub RadButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadButton1.Click
    Dim form As New RadForm1
    If (form.ShowDialog() = DialogResult.OK) Then
        Me.BackgroundWorker1.RunWorkerAsync()
    End If
End Sub

But sadly, i tried everything i could have thought of, suspending the parent grid layout, but everything was working, i am betting on a threading issue somewhere, but without more code, cannot say anything for certain.

Hope this helps,
Best Regards,
Emanuel Varga
0
Richard Slade
Top achievements
Rank 2
answered on 14 Oct 2010, 11:28 AM
Hi Emanuel, 
Thanks - and yes, I spotted that I had left in that line too and had edited my post to remove it. 


Sachin,
are you using a simple background worker, or creating your own thread manually? 

All the best 
Richard
0
Emanuel Varga
Top achievements
Rank 1
answered on 14 Oct 2010, 12:14 PM
Hello again,

I cannot get it to fail...
I have prepared an example that closer resembles your case, you can set if you want to receive data from a background thread or directly, and I've also set up a case in which the data source for the main grid is being sent onto the child form and when you press save it will call a refresh on the main grid, but you can test it for yourself and tell me if you are doing something different in your application:
Imports System
Imports System.Drawing
Imports System.Threading
Imports System.Windows.Forms
Imports Telerik.WinControls.UI
Imports Telerik.WinControls.UI.Docking
Imports System.Collections.Generic
Partial Public Class Form1
    Inherits Form
    Private WithEvents radGridView1 As RadGridView
 
    Private radDock1 As RadDock
 
    Private synchronizationContext As SynchronizationContext
 
    Public Sub New()
        radDock1 = New RadDock()
        synchronizationContext = synchronizationContext.Current
    End Sub
 
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        radDock1.Dock = DockStyle.Fill
        Dim dockWindow = New DocumentWindow("Dock1")
        Me.Controls.Add(radDock1)
        radDock1.AddDocument(dockWindow)
 
        radGridView1 = New RadGridView()
        radGridView1.[ReadOnly] = True
        radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill
        radGridView1.Dock = DockStyle.Fill
        dockWindow.Controls.Add(radGridView1)
 
        radGridView1.DataSource = New TestsCollection(1, 10)
    End Sub
 
    Private Sub radGridView1_DataBindingComplete(ByVal sender As Object, ByVal e As GridViewBindingCompleteEventArgs) Handles radGridView1.DataBindingComplete
 
        If (radGridView1.Columns.Contains("Command")) Then
            Return
        End If
 
        Dim column1 = New GridViewCommandColumn("Command")
        radGridView1.Columns.Add(column1)
    End Sub
 
    Private Sub radGridView1_CommandCellClick(ByVal sender As Object, ByVal e As EventArgs) Handles radGridView1.CommandCellClick
        Using form = New Form2(Me, radGridView1.DataSource)
            form.ShowDialog()
        End Using
    End Sub
 
    Public Sub SimulateDataChangedEvent(ByVal differentThread As Boolean)
        If differentThread Then
            Dim thread = New Thread(AddressOf RefreshDataFromADifferentThread)
            thread.IsBackground = True
            thread.Start(radGridView1.DataSource)
        Else
            SetNewDataSource()
 
        End If
    End Sub
 
    Private Sub RefreshDataFromADifferentThread(ByVal dataObject As Object)
        If Me.radGridView1.InvokeRequired Then
            synchronizationContext.Post(AddressOf RefreshDataFromADifferentThread, dataObject)
            Return
        End If
        SetNewDataSource()
 
    End Sub
 
    Private Sub SetNewDataSource()
        ' get the new data here;
        radGridView1.DataSource = New TestsCollection(100, 100)
 
        'else if it's the same instance just refresh
        'radGridView1.Refresh()
    End Sub
End Class
 
#Region "Second Form"
 
Public Class Form2
    Inherits Form
    Private dataSource As Object
    Private parentRadForm As Form1
    Dim WithEvents okButton As New RadButton()
 
    Private radGridView1 As RadGridView
 
    Public Sub New(ByVal sender As Object, ByVal dataSource As Object)
        InitializeComponent()
        parentRadForm = DirectCast(sender, Form1)
        Me.dataSource = dataSource
    End Sub
 
    Private Sub Form2_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        okButton.Text = "Changes ok, call the data changed Event"
        okButton.Dock = DockStyle.Bottom
        Me.Controls.Add(okButton)
 
        radGridView1 = New RadGridView()
        radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill
 
        radGridView1.DataSource = dataSource
 
        radGridView1.Size = New Size(Me.Width, Me.Height - 62)
        radGridView1.Anchor = AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Top Or AnchorStyles.Bottom
 
        Me.Controls.Add(radGridView1)
    End Sub
 
    Private Sub okButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles okButton.Click
        parentRadForm.SimulateDataChangedEvent(True)
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
    End Sub
 
    ''' <summary>
    ''' Required designer variable.
    ''' </summary>
    Private components As System.ComponentModel.IContainer
 
    ''' <summary>
    ''' Clean up any resources being used.
    ''' </summary>
    ''' <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso (components IsNot Nothing) Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub
 
#Region "Windows Form Designer generated code"
 
    ''' <summary>
    ''' Required method for Designer support - do not modify
    ''' the contents of this method with the code editor.
    ''' </summary>
    Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container()
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.Text = "Form1"
    End Sub
 
#End Region
End Class
 
#End Region
 
#Region "Business Objects"
 
Public Class Test
    Public Property Id() As Integer
        Get
            Return m_Id
        End Get
        Set(ByVal value As Integer)
            m_Id = value
        End Set
    End Property
    Private m_Id As Integer
 
    Public Property Name() As String
        Get
            Return m_Name
        End Get
        Set(ByVal value As String)
            m_Name = value
        End Set
    End Property
    Private m_Name As String
 
    Public Sub New(ByVal id As Integer, ByVal name As String)
        Me.Id = id
        Me.Name = name
    End Sub
End Class
 
Public Class TestsCollection
    Inherits List(Of Test)
    Public Sub New(ByVal startIndex As Integer, ByVal noItems As Integer)
        For i As Integer = startIndex To startIndex + noItems - 1
            Me.Add(New Test(i, "test" + i.ToString))
        Next
    End Sub
End Class
 
#End Region

Hope this helps, if you have any other questions or comments, please let me know,

Best Regards,
Emanuel Varga
Tags
GridView
Asked by
SachinC
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
SachinC
Top achievements
Rank 1
Julian Benkov
Telerik team
Richard Slade
Top achievements
Rank 2
Share this question
or