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

Refreshing and Retaining Current Row

2 Answers 1468 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Rick
Top achievements
Rank 1
Rick asked on 01 Aug 2014, 04:34 PM
I have a couple of issues, but they are all related so I am putting them all in one post.

I have a form with a GridView on it showing several records.  The user selects a record and opens a new form where they can edit all of the values.  After saving, they return to the main form with the grid where I would like to update the grid to display the data the just edited.  I want the grid to retain the current row, or rather, the row for the record they just edited.

Additionally, the user can also add a new record, so after saving and returning to the form, the grid is updated and I want the newly created record to be the current row.

I am programming in MS Visual Studio Pro 2012 version 11.0.50727
with Microsoft .NET Framework version 4.5.50938
I am coding in Visual Basic.
My Telerik version is 2014.2.715.40

My radGridView gets it's data using a DataSet, BindingSOurce, and a TableAdaptor.
My data is coming from SQL Server 10.50.2500

On form load, my gridview gets its data with this line of code:

Me.RadGridView1.DataSource = Me.AutoEmailerTBLTableAdapter.GetData(Environment.UserName, Environment.UserName)

To sum up, I want to:

A - Refresh grid and retain current row
B - Refresh grid and set current row to newly added record





2 Answers, 1 is accepted

Sort by
0
Rick
Top achievements
Rank 1
answered on 04 Aug 2014, 06:49 PM
I found a way to do this.  I'm not sure it's the fastest way, but it works.  If there is a better or more efficient way I'd appreciate it.

In my edit form, I set the value of glNewID to the edited record or the new record.  My edit form is modal, so when it closes I run this line to refresh the grid:

Me.RadGridView1.DataSource = Me.AutoEmailerTBLTableAdapter.GetData(Environment.UserName, Environment.UserName)

Then I have this code which loops the rows looking for the value:

Private Sub RadGridView1_DataBindingComplete(sender As Object, e As GridViewBindingCompleteEventArgs) Handles RadGridView1.DataBindingComplete
 
    For x = 0 To RadGridView1.Rows.Count - 1
        If Me.RadGridView1.ChildRows(x).Cells("AutoEmailer_ID").Value = glNewID Then
            RadGridView1.ChildRows(x).IsCurrent = True
            Exit For
        End If
    Next
 
End Sub



0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 06 Aug 2014, 09:54 AM
Hello Rick,

Thank you for writing.

The MasterTemplate.SelectLastAddedRow property gets or sets a value indicating whether the last added row in the RadGridView.DataSource will be selected by the control. This property may be useful for your question related to setting the current row to newly added record.

However, when updating items in the DataSource, RadGridView should be notified for these changes in order to avoid rebinding/refreshing the master template. I think that our Reflecting Custom Object Changes in RGV help article may be useful about this case. When I use a data table from a sql server directly bound to the RadGridView, modifying the specific row in the data source directly affects the RadGridView without the necessity of refreshing or rebinding. Please refer to the attached RefreshChanges.gif.

However, if your requirement is to keep the current row before changing the DataSource, you can just store a unique cell value from the row or its index and restore it afterwards. Keeping a unique cell value requires iterating through all the rows and setting the IsCurrent property similar to your sample code. However, if you keep the index, you can directly set the current row:
int currentRowIndex = this.radGridView1.CurrentRow.Index;
//perform changes as refreshing/rebinding
this.radGridView1.CurrentRow = this.radGridView1.Rows[currentRowIndex];

I hope this information helps. Should you have further questions, I would be glad to help.
 
Regards,
Desislava
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
Tags
GridView
Asked by
Rick
Top achievements
Rank 1
Answers by
Rick
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or