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

Using dynamic sources

6 Answers 164 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Tim
Top achievements
Rank 1
Tim asked on 21 Dec 2010, 06:55 PM
Maybe I'm setting up the grid the wrong way but what I need to do is to constantly query some external files to build a grid. That isn't the issue per se as I've done that. But every time I rebuild the grid (which is around every 2 seconds) what ever row I had selected and where ever I had scrolled to in the list goes away and I go back to the top of the list.

Every two seconds a BuildGrid procedure is called where the data source is created and assigned to the grid.

Here is a portion of the BuildGrid that I'm referring to:

   Private Sub BuildGrid()
        Timer1.Stop()
        Dim completedJobSource As New BindingSource
        While not done
              completedJobSource.Add(New CompletedJobs(strGUID, strJobName, strJobDescription, priority))
         end while

    RadGridView_Completed.DataSource = completedJobSource
   Timer.Start

Could it be that the data source is always being created here? It is possible that some of the data rows might not be there the next time which isn't a big deal but I just don't like that the scrolling keeps going to the top. It would also be great to keep the current row selected whatever it was whether the data in it changed or not.

Thanks,

Tim

6 Answers, 1 is accepted

Sort by
0
Richard Slade
Top achievements
Rank 2
answered on 21 Dec 2010, 11:52 PM
Hi,

The one potential issue I can think if with refreshing the the datasource and keeping the scroll position and selected row is if you are in the middle of scrolling through rows then the grid would still scroll back to another position. Is this acceptable? If so, I will try and put together a small sample for you.
Regards,
Richard
0
Tim
Top achievements
Rank 1
answered on 22 Dec 2010, 12:03 AM
Thanks. I don't think that's a big deal. And I see there's a "_Scroll" event for the grid so perhaps I can pause the clock or updates while scrolling so no updates occur if that is how that event is triggered.
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 22 Dec 2010, 10:04 AM
Hi Tim,

Please try this (it's just a radgridview and a timer on a form) and let me know how this is for you.
Imports System.ComponentModel
Imports Telerik.WinControls.Data
Imports Telerik.WinControls.UI.Export
  
Public Class Form1
  
    Private m_CurrentRowIndex As Integer
  
  
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Timer1.Interval = 2000
  
        Me.RadGridView1.Columns.Add("Name")
        Me.RadGridView1.Columns.Add("Age")
  
        Me.Timer1.Start()
    End Sub
  
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        BuildGrid()
    End Sub
  
    Private Sub BuildGrid()
        Me.Timer1.Stop()
        RemoveHandler RadGridView1.SelectionChanged, AddressOf RadGridView1_SelectionChanged
        Dim source As New BindingList(Of Person)
        For i As Integer = 40 To 50
            source.Add(New Person("Richard", i))
            source.Add(New Person("Fred", i + 2))
            source.Add(New Person("Chris", i + 4))
            source.Add(New Person("Peter", i + 6))
            source.Add(New Person("Stewart", i + 8))
            source.Add(New Person("Oliver", i + 10))
            source.Add(New Person("Richard", i))
            source.Add(New Person("Fred", i + 2))
            source.Add(New Person("Chris", i + 4))
            source.Add(New Person("Peter", i + 6))
            source.Add(New Person("Stewart", i + 8))
            source.Add(New Person("Oliver", i + 10))
        Next
        Me.RadGridView1.BeginUpdate()
        Me.RadGridView1.DataSource = source
  
        Me.RadGridView1.EndUpdate()
        If Me.RadGridView1.RowCount - 1 >= m_CurrentRowIndex Then
            Me.RadGridView1.Rows(m_CurrentRowIndex).IsCurrent = True
            Me.RadGridView1.Rows(m_CurrentRowIndex).IsSelected = True
            If Not Me.RadGridView1.TableElement.IsRowVisible(Me.RadGridView1.Rows(m_CurrentRowIndex)) Then
                Me.RadGridView1.TableElement.EnsureRowVisible(Me.RadGridView1.Rows(m_CurrentRowIndex))
            End If
        End If
        AddHandler RadGridView1.SelectionChanged, AddressOf RadGridView1_SelectionChanged
        Me.Timer1.Start()
    End Sub
  
    Private Sub RadGridView1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadGridView1.SelectionChanged
        If Me.RadGridView1.CurrentRow.Index > -1 Then
            Me.m_CurrentRowIndex = Me.RadGridView1.CurrentRow.Index
        End If
    End Sub
  
End Class
  
Public Class Person
    Public Sub New(ByVal name As String, ByVal age As Integer)
        Me.Name = name
        Me.Age = age
    End Sub
  
    Public Property Name As String
    Public Property Age As Integer
  
End Class

Hope that helps
Richard
0
Tim
Top achievements
Rank 1
answered on 22 Dec 2010, 07:32 PM
Thanks Richard. That helped. I modified a portion of it slightly to store the vScroll and hScroll values to keep the scroll position yet the selected item remains. The change I made is below for those who might be interested.

In your example if you have filtering turned on and you're typing in a filter in the edit box, do you get an exception during the build? I get them (perhaps at  Telerik.WinControls.UI.GridCellElement.get_ViewTemplate() ) during the build (but not when the build is complete).  Putting  EnableFiltering = False and then True would fix it but it would be pretty ugly as it would continually hide & show. The other option which I'm working on now is just building a custom filter form which if it has values set, will compare when it's time to add data to the source and if it meets the criteria then it gets added to the data source, if not, then it won't. Not as fluent as the built-in filter but it should work in the short term.

One last question - is there a property to just select the row without the 'border' around the column you clicked on? I just want to treat the row as one long piece of data and seeing the column that was clicked isn't really neccessary. Thanks.

-Tim

Dim vScroll As Integer = Me.RadGridView_Completed.TableElement.VScrollBar.Value
Dim hScroll As Integer = Me.RadGridView_Completed.TableElement.HScrollBar.Value
 
RadGridView_Completed.BeginUpdate()
RadGridView_Completed.DataSource = completedJobSource
RadGridView_Completed.EndUpdate()
If Me.RadGridView_Completed.RowCount - 1 >= m_CurrentRowIndex Then
    Me.RadGridView_Completed.Rows(m_CurrentRowIndex).IsCurrent = True
    Me.RadGridView_Completed.Rows(m_CurrentRowIndex).IsSelected = True
    Me.RadGridView_Completed.TableElement.VScrollBar.Value = vScroll
    Me.RadGridView_Completed.TableElement.HScrollBar.Value = hScroll
    'If Not Me.RadGridView_Completed.TableElement.IsRowVisible(Me.RadGridView_Completed.Rows(m_CurrentRowIndex)) Then
    '    Me.RadGridView_Completed.TableElement.EnsureRowVisible(Me.RadGridView_Completed.Rows(m_CurrentRowIndex))
    'End If
End If
AddHandler RadGridView_Completed.SelectionChanged, AddressOf RadGridView_Completed_SelectionChanged



0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 23 Dec 2010, 11:14 AM
Hi,

I'm glad that has helped.
To answer your last question, first set the following property
Me.RadGridView1.SelectionMode = GridViewSelectionMode.FullRowSelect

then subscribe to the ViewCellFormatting event and add the following
Private Sub RadGridView1_CellFormatting(ByVal sender As System.Object, ByVal e As Telerik.WinControls.UI.CellFormattingEventArgs) Handles RadGridView1.ViewCellFormatting
    If TypeOf e.CellElement Is GridDataCellElement Or TypeOf e.CellElement.ColumnInfo Is GridViewRowHeaderColumn Then
        e.CellElement.DrawBorder = False
    End If
End Sub

Please remember to mark helpful suggestions as answer and if you need anything further just let me know
Richard
0
Tim
Top achievements
Rank 1
answered on 27 Dec 2010, 07:35 PM
Perfect. Thank you.
Tags
GridView
Asked by
Tim
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Tim
Top achievements
Rank 1
Share this question
or