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

scroll bar position after refreshing grid

11 Answers 1065 Views
GridView
This is a migrated thread and some comments may be shown as answers.
muralidhar
Top achievements
Rank 1
muralidhar asked on 08 Mar 2012, 11:45 AM

Hi,

I am working on the hierarchical grid and displaying data using self references.

My requirement is when i refresh the grid then the selectd row should be selected and the scroll bar
position should be at the same location but now when i refresh the scroll bar is moved to top of the grid.

could you please help me on the above requirement?

Thanks & Regards
Muralidhar Dasari

Code Snippet for refresh:- 
using (radGridView1.DeferRefresh())
  {
    this.radGridView1.TableElement.Update(GridUINotifyAction.DataChanged);
    this.radGridView1.TableElement.Update(GridUINotifyAction.StateChanged);
}

11 Answers, 1 is accepted

Sort by
0
Svett
Telerik team
answered on 13 Mar 2012, 09:33 AM
Hi Muralidhar,

When you are using the DeferRefresh method, you are causing the RadGridView control to reset its visual state. Then it sets the scroll-bar position in the initial state. You should use only the code snippet below:

this.radGridView1.TableElement.Update(GridUINotifyAction.DataChanged);
this.radGridView1.TableElement.Update(GridUINotifyAction.StateChanged);

I hope this helps.

All the best,
Svett
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
0
muralidhar
Top achievements
Rank 1
answered on 14 Mar 2012, 06:02 AM

Hi,

Thanks for the response, it is working fine if i have no new rows in my data source but if new rows are there in my grid after refresh then the scroll bar is moved to last last row of the grid.

can you help me in positioning the scroll bar at the same location?

Thanks & Regards
Muralidhar Dasari.

 

0
Svett
Telerik team
answered on 16 Mar 2012, 10:45 AM
Hi Muralidhar,

I am not able to assist you efficiently with the supplied information. Would you pelase share with us the full code snippet where you are using the Update method of GridViewTableElement? Hence, we will understand your scenario in depth and will be able to assist you.

All the best,
Svett
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
0
muralidhar
Top achievements
Rank 1
answered on 19 Mar 2012, 06:27 AM
Hi,

I am displayed the data in the grid in  hierarchical format.
I have a button on the form to refresh the data in the grid but the grid is always scrolled to first postion after refresh.
private static DataTable CreateDataSource() 
        
            DataTable dataSource = new DataTable("fileSystem");
            dataSource.Columns.Add("ID", typeof(int));
            dataSource.Columns.Add("ParentID", typeof(int));
            dataSource.Columns.Add("Name", typeof(string)); 
            dataSource.Columns.Add("Date", typeof(DateTime));
            dataSource.Columns.Add("Type", typeof(string)); 
            dataSource.Columns.Add("Size", typeof(int)); 
            dataSource.Rows.Add(1, null, "Program Files", DateTime.Now.AddDays(-100), "Folder", 5120); 
            dataSource.Rows.Add(2, 1, "Visual Studio 2010", DateTime.Now.AddDays(-100), "Folder", 3220); 
            dataSource.Rows.Add(3, 2, "bin", DateTime.Now.AddDays(-100), "Folder", 3220); 
            dataSource.Rows.Add(4, 2, "READEME.txt", DateTime.Now.AddDays(-100), "Text Document", 3); 
            dataSource.Rows.Add(5, 1, "Telerik RadControls", DateTime.Now.AddDays(-10), "Folder", 3120); 
            dataSource.Rows.Add(6, 5, "RadControls for Winforms", DateTime.Now.AddDays(-10), "Folder", 101); 
            dataSource.Rows.Add(7, 5, "RadControls for Silverlight", DateTime.Now.AddDays(-10), "Folder", 123); 
            dataSource.Rows.Add(8, 5, "RadControls for WPF", DateTime.Now.AddDays(-10), "Folder", 221);
            dataSource.Rows.Add(9, 5, "RadControls for ASP.NET AJAX", DateTime.Now.AddDays(-10), "Folder", 121); 
            dataSource.Rows.Add(10, 1, "Microsoft Office 2010", DateTime.Now.AddDays(-120), "Folder", 1230);
            dataSource.Rows.Add(11, 10, "Microsoft Word 2010", DateTime.Now.AddDays(-120), "Folder", 1230); 
            dataSource.Rows.Add(12, 10, "Microsoft Excel 2010", DateTime.Now.AddDays(-120), "Folder", 1230); 
            dataSource.Rows.Add(13, 10, "Microsoft Powerpoint 2010", DateTime.Now.AddDays(-120), "Folder", 1230); 
            dataSource.Rows.Add(14, 1, "Debug Diagnostic Tools v1.0", DateTime.Now.AddDays(-400), "Folder", 2120); 
            dataSource.Rows.Add(15, 1, "Designer's 3D Tools", DateTime.Now.AddDays(-500), "Folder", 1120); 
            dataSource.Rows.Add(16, 1, "Communication", DateTime.Now.AddDays(-700), "Folder", 120);
            dataSource.Rows.Add(17, null, "My Documents", DateTime.Now.AddDays(-200), "Folder", 1024);
            dataSource.Rows.Add(18, 17, "Salaries.xlsx", DateTime.Now.AddDays(-200), "Excel File", 1); 
            dataSource.Rows.Add(19, 17, "RecessionAnalysis.xlsx", DateTime.Now.AddDays(-200), "Excel File", 1);
            dataSource.Rows.Add(20, null, "Windows", DateTime.Now.AddDays(-100), "Folder", 10240); 
            dataSource.Rows.Add(21, 20, "System32", DateTime.Now.AddDays(-220), "Folder", 510); 
            dataSource.Rows.Add(22, 20, "assembly", DateTime.Now.AddDays(-20), "Folder", 240); 
            dataSource.Rows.Add(23, 22, "System.Data.dll", DateTime.Now.AddDays(-20), "Assembly File", 4); 
            dataSource.Rows.Add(24, 22, "System.Core.dll", DateTime.Now.AddDays(-20), "Assembly File", 2); 
            dataSource.Rows.Add(25, 22, "System.Drawings.dll", DateTime.Now.AddDays(-20), "Assembly File", 3); 
            dataSource.Rows.Add(26, 22, "Telerik.WinControls.UI.dll", DateTime.Now.AddDays(-20), "Assembly File", 5);
            dataSource.Rows.Add(27, null, "Users", DateTime.Now.AddDays(-100), "Folder", 5512);
            dataSource.Rows.Add(28, 27, "Administrator", DateTime.Now.AddDays(-100), "Folder", 1512); 
            dataSource.Rows.Add(29, 27, "Guest", DateTime.Now.AddDays(-100), "Folder", 2512);
            dataSource.Rows.Add(30, 27, "User1", DateTime.Now.AddDays(-100), "Folder", 3512);
            dataSource.Rows.Add(31, null, "Share", DateTime.Now.AddDays(-50), "Folder", 15360); 
            dataSource.Rows.Add(32, 31, "Photos", DateTime.Now.AddDays(-50), "Folder", 360);
            dataSource.Rows.Add(33, 32, "Flowers.JPG", DateTime.Now.AddDays(-50), "JPEG File", 1);
            dataSource.Rows.Add(34, 32, "Panda.GIF", DateTime.Now.AddDays(-50), "GIF File", 3); 
            dataSource.Rows.Add(35, 32, "Landscape.png", DateTime.Now.AddDays(-50), "PNG File", 4); 
            dataSource.Rows.Add(36, null, "Music", DateTime.Now.AddDays(-2), "Folder", 0); 
            dataSource.Rows.Add(37, 36, "Mozart", DateTime.Now.AddDays(-3), "Folder", 0); 
            dataSource.Rows.Add(38, 36, "Pavarotti", DateTime.Now.AddDays(-40), "Folder", 0); 
            dataSource.Rows.Add(39, 36, "AC/DC", DateTime.Now.AddDays(-50), "Folder", 0); 
            dataSource.Rows.Add(40, 36, "Queen", DateTime.Now.AddDays(-8), "Folder", 0); 
            dataSource.Rows.Add(33, null, "Boot.ini", DateTime.Now.AddDays(-10), "INI File", 0);
  
  
  
            dataSource.Rows.Add(41, null, "Music1", DateTime.Now.AddDays(-2), "Folder", 0);
            dataSource.Rows.Add(42, 41, "Mozart1", DateTime.Now.AddDays(-3), "Folder", 0);
            dataSource.Rows.Add(43, 41, "Pavarotti1", DateTime.Now.AddDays(-40), "Folder", 0);
            dataSource.Rows.Add(44, 41, "AC/DC1", DateTime.Now.AddDays(-50), "Folder", 0);
            dataSource.Rows.Add(45, 41, "Queen1", DateTime.Now.AddDays(-8), "Folder", 0);
           
  
  
            dataSource.Rows.Add(46, null, "Music2", DateTime.Now.AddDays(-2), "Folder", 0);
            dataSource.Rows.Add(47, 46, "Mozart2", DateTime.Now.AddDays(-3), "Folder", 0);
            dataSource.Rows.Add(48, 46, "Pavarotti2", DateTime.Now.AddDays(-40), "Folder", 0);
            dataSource.Rows.Add(49, 46, "AC/DC2", DateTime.Now.AddDays(-50), "Folder", 0);
            dataSource.Rows.Add(50, 46, "Queen2", DateTime.Now.AddDays(-8), "Folder", 0);
  
  
            dataSource.Rows.Add(51, null, "Murali 3", DateTime.Now.AddDays(-2), "Folder", 0);
            dataSource.Rows.Add(52, 51, "Mozart 3", DateTime.Now.AddDays(-3), "Folder", 0);
            dataSource.Rows.Add(53, 51, "Pavarotti 3", DateTime.Now.AddDays(-40), "Folder", 0);
            dataSource.Rows.Add(54, 51, "AC/DC2 3", DateTime.Now.AddDays(-50), "Folder", 0);
            dataSource.Rows.Add(55, 51, "Queen 3", DateTime.Now.AddDays(-8), "Folder", 0);
  
  
            dataSource.Rows.Add(56, null, "Murali 4", DateTime.Now.AddDays(-2), "Folder", 0);
            dataSource.Rows.Add(57, 56, "Mozart 3", DateTime.Now.AddDays(-3), "Folder", 0);
            dataSource.Rows.Add(58, 56, "Pavarotti 3", DateTime.Now.AddDays(-40), "Folder", 0);
            dataSource.Rows.Add(59, 56, "AC/DC2 3", DateTime.Now.AddDays(-50), "Folder", 0);
            dataSource.Rows.Add(60, 56, "Queen 3", DateTime.Now.AddDays(-8), "Folder", 0);
  
            dataSource.Rows.Add(61, null, "Murali 5", DateTime.Now.AddDays(-2), "Folder", 0);
            dataSource.Rows.Add(62, 61, "Mozart 3", DateTime.Now.AddDays(-3), "Folder", 0);
            dataSource.Rows.Add(63, 61, "Pavarotti 3", DateTime.Now.AddDays(-40), "Folder", 0);
            dataSource.Rows.Add(64, 61, "AC/DC2 3", DateTime.Now.AddDays(-50), "Folder", 0);
            dataSource.Rows.Add(65, 61, "Queen 3", DateTime.Now.AddDays(-8), "Folder", 0);
  
            return dataSource; 
        }
private void Form1_Load(object sender, EventArgs e)
        {
 this.radGridView1.AllowAddNewRow = false;
            this.radGridView1.EnableFiltering = true;
               
            this.radGridView1.MasterTemplate.ShowHeaderCellButtons = true; 
            this.radGridView1.MasterTemplate.ShowFilteringRow = false; 
            this.radGridView1.TableElement.RowHeight = 40;
            this.radGridView1.ViewCellFormatting += new CellFormattingEventHandler(radGridView1_ViewCellFormatting);
            this.radGridView1.CellFormatting += new CellFormattingEventHandler(radGridView1_CellFormatting);
            this.radGridView1.ReadOnly = true;
            this.radGridView1.MasterTemplate.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
            this.radGridView1.AutoGenerateColumns = false;
            _dataTable = CreateDataSource();
            this.radGridView1.DataSource = _dataTable;
            this.radGridView1.Relations.AddSelfReference(this.radGridView1.MasterTemplate, "ID", "ParentID"); 
            this.radGridView1.Columns["ID"].IsVisible = false;
            this.radGridView1.Columns["ParentID"].IsVisible = false;
            this.radGridView1.Columns["Size"].FormatString = "{0} MB";
            this.radGridView1.MasterTemplate.ExpandAll();
}
In Button click i have written the below code:-
//Refresh
        private void button3_Click(object sender, EventArgs e)
        {
 _dataTable.Rows.Add(66, null, "Murali 3", DateTime.Now.AddDays(-2), "Folder", 0);
            _dataTable.Rows.Add(67, 66, "Mozart 3", DateTime.Now.AddDays(-3), "Folder", 0);
            _dataTable.Rows.Add(68, 66, "Pavarotti 3", DateTime.Now.AddDays(-40), "Folder", 0);
            _dataTable.Rows.Add(69, 66, "AC/DC2 3", DateTime.Now.AddDays(-50), "Folder", 0);
            _dataTable.Rows.Add(70, 66, "Queen 3", DateTime.Now.AddDays(-8), "Folder", 0);
  this.radGridView1.TableElement.Update(GridUINotifyAction.DataChanged);
            this.radGridView1.TableElement.Update(GridUINotifyAction.StateChanged);
}

when i click the button then the grid is scrolled to top of the screen but i want the scroll bar to remain at the same position.

Thanks & Regards
Muralidhar Dasari


0
Accepted
Svett
Telerik team
answered on 21 Mar 2012, 02:30 PM
Hi Muralidhar,

This is the expected behavior. Nevertheless, you can change it by using the following code snippet in the button click event handler:

RadScrollBarElement vScrollBar = this.radGridView1.TableElement.VScrollBar;
int scrollBarValue = vScrollBar.Value;
 
_dataTable.Rows.Add(66, null, "Murali 3", DateTime.Now.AddDays(-2), "Folder", 0);
_dataTable.Rows.Add(67, 66, "Mozart 3", DateTime.Now.AddDays(-3), "Folder", 0);
_dataTable.Rows.Add(68, 66, "Pavarotti 3", DateTime.Now.AddDays(-40), "Folder", 0);
_dataTable.Rows.Add(69, 66, "AC/DC2 3", DateTime.Now.AddDays(-50), "Folder", 0);
_dataTable.Rows.Add(70, 66, "Queen 3", DateTime.Now.AddDays(-8), "Folder", 0);
this.radGridView1.TableElement.Update(GridUINotifyAction.DataChanged);
this.radGridView1.TableElement.Update(GridUINotifyAction.StateChanged);
this.radGridView1.TableElement.RowScroller.UpdateScrollRange();
 
this.radGridView1.TableElement.VScrollBar.Value = this.ClampValue(scrollBarValue,
                                        vScrollBar.Minimum,
                                        vScrollBar.Maximum - vScrollBar.LargeChange + 1);

private int ClampValue(int value, int minimum, int maximum)
{
    if (value < minimum)
    {
        return minimum;
    }
    if (maximum > 0 && value > maximum)
    {
        return maximum;
    }
    return value;
}

I hope this helps.

All the best,
Svett
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
0
Guy
Top achievements
Rank 1
answered on 01 Nov 2014, 09:51 PM
Hi Svett,
I'm referring to your solution for scrolling correctly after data update. That's work perfectly...but

 I've an additional issue i.e. when sorting the column A or c... (with headers column)  before updating .
Let us say I update one row and then my LinQ Query  restores the data grid sorted by Column B.
When updating and scrolling, it's referring to a VscrollBar.Value that's nothing to do with the original sorted state.

I understand this behaviour is normal but how can I cope with this  in order to  programmatically retrieve the sorted view and/or  the correct position row after refreshing the data grid.

Thanks for your help.
regards.



RadScrollBarElement vScrollBar = this.radGridView1.TableElement.VScrollBar;
int scrollBarValue = vScrollBar.Value;
0
George
Telerik team
answered on 05 Nov 2014, 01:24 PM
Hi Guy,

Thank you for writing.

I do not think that I understood your question from the provided information. Can you please provide a more detailed information about the scenario at hand? Some specific steps would be very helpful.

Looking forward to your reply.

Regards,
George
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Guy
Top achievements
Rank 1
answered on 06 Nov 2014, 10:14 PM
Here is my issue. I built a small app. based on previous data in these post. 

When I click  to "Add lines" , I've no problem and the solution explained before worked perfectly, retrieving the selected rows even if it is sorted .
Now, I want to update my data source (here the table but could be a linQ Query or anything else) and I click the radButton2
 I lost my selected row. Maybe this procedure does not suit this case, and I should keep the ID  line selected and try to retrieve this ID line when I recover the data grid ?Any idea is welcome to solve this.
private void radButton2_Click(object sender, EventArgs e)
{
    // to reload the datatable
    RadScrollBarElement vScrollBar = this.radGridView1.TableElement.VScrollBar;
    int scrollBarValue = vScrollBar.Value;
 
    DataTable _dataTable = new DataTable();
   
    // here should come some updates in the table and then reload  to datagrid
    _dataTable = CreateDataSource();
 
    this.radGridView1.DataSource = _dataTable;
 
    this.radGridView1.MasterTemplate.ExpandAll();
 
    this.radGridView1.TableElement.Update(GridUINotifyAction.DataChanged);
    this.radGridView1.TableElement.Update(GridUINotifyAction.StateChanged);
    this.radGridView1.TableElement.RowScroller.UpdateScrollRange();
 
    this.radGridView1.TableElement.VScrollBar.Value = this.ClampValue(scrollBarValue,
                                            vScrollBar.Minimum,
                                            vScrollBar.Maximum - vScrollBar.LargeChange + 1);
 
}
0
George
Telerik team
answered on 11 Nov 2014, 01:30 PM
Hello Guy,

Thank you for your reply.

You can save the selected index of the row and set it after the data is reloaded:
private void Button_Click1(object sender, EventArgs e)
{
    int index = this.Grid.CurrentRow.Index;
 
    //load the data
 
    this.Grid.CurrentRow = this.Grid.Rows[index];
}

I hope this will help.

Regards,
George
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Guy
Top achievements
Rank 1
answered on 11 Nov 2014, 04:28 PM
Hi George,
thanks for your answer but your solution doesn't work and I don't know why because it seems to be so simple.
here is my code modified with your recommendation. 
                int index = this.radGridView1.CurrentRow.Index;
            RadMessageBox.Show("Index = " + index );
            DataTable _dataTable = new DataTable();
            _dataTable = CreateDataSource();
            this.radGridView1.DataSource = _dataTable;
            this.radGridView1.MasterTemplate.ExpandAll();
            this.radGridView1.CurrentRow = this.radGridView1.Rows[index];

I've insert a message Box to see the index and as you can see in the attach image, this index is not relevant. when I reload the grid I'm positioned on the fifth line which is not the previous one.
I don't understand. !
 Thanks for your support. Best regards.

0
George
Telerik team
answered on 14 Nov 2014, 09:42 AM
Hello Guy,

Thank you for your reply.

You can find attached a project which saves the current row and after the grid has its data source changed the same row is selected. Let me know if there is anything that I am missing. If you need to attach a sample project, feel free to submit a new support ticket.

Looking forward to your reply.

Regards,
George
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
GridView
Asked by
muralidhar
Top achievements
Rank 1
Answers by
Svett
Telerik team
muralidhar
Top achievements
Rank 1
Guy
Top achievements
Rank 1
George
Telerik team
Share this question
or