Hi,
I'm trying to figure out how to set the ActiveRow to the same scrolled position after data updates. Example if it's in the 5th position before rows aer added or deleted I want it displayed back in the 5th position (viewindex). The ScrollPosition of the row is not accessable.
Please look at the enclosed example and tell me where I've gone awry.
//random generated junk from mockaroo.com #region string sampleData = @"6,Thomas,Hunter,thunter5@ucoz.com,Male,124.126.178.0, 7,Aaron,Robertson,arobertson6@squarespace.com,Male,206.206.106.42, 8,Judith,Price,jprice7@edublogs.org,Female,215.33.252.254, 9,Howard,Ortiz,hortiz8@go.com,Male,122.12.248.126, 10,Douglas,Snyder,dsnyder9@amazonaws.com,Male,28.22.144.247".Trim(); #endregion
string[] values = sampleData.Split(',');
UltraGridRow prevRow = this.ultraGrid1.Rows.OfType<UltraGridRow>() .ToList() .Find(r => r.Cells[0].Text.Equals(_activeId.ToString(), StringComparison.CurrentCultureIgnoreCase));
for (int index = 0; index < values.Count(); index += 6) { var data = values.Skip(index).Take(6).Select(x => x.ToString().Trim()).ToArray(); _sampleData.Add(new SampleData() { ID = Convert.ToInt16(data[0]), FirstName = data[1].ToString(), LastName = data[2].ToString(), Email = data[3].ToString(), Gender = data[4].ToString(), IPAddress = data[5].ToString() }); }
_sampleData = _sampleData.OrderBy(o => o.ID).ToList();
ultraDataSource1.Rows.SetCount(_sampleData.Count); ultraDataSource1.ResetCachedValues();
UltraGridRow row = this.ultraGrid1.Rows.OfType<UltraGridRow>() .ToList().Find(r => r.Cells[0].Text.Equals(_activeId.ToString(), StringComparison.CurrentCultureIgnoreCase));
ultraGrid1.ActiveRow = row; var activeIndex = ultraGrid1.ActiveRow.Index;
ultraGrid1.ActiveRowScrollRegion.ScrollPosition = prevRow.VisibleIndex; ultraGrid1.Rows[activeIndex].Activate(); ultraGrid1.Rows[activeIndex].Selected = true; ultraGrid1.ActiveRowScrollRegion.ScrollRowIntoView(row);
Thanks,
Glenn
Hi Glenn,
After taking a closer look at this, I feel that it's generally not a good idea to do, since it can cause a host of issues and would be difficult to maintain.
One issue is that the active row before a data update may not be the same instance of the UltraGridRow object after a data update. If new data is bound to the grid, it will be necessary to figure out which row you are looking for. You might be able to accomplish this by using a primary key value to identify the row, but it will not be 100% reliable. There is also, of course, no guarantee that the old active row even exists in the new data.
Another issue occurs if rows are added or deleted in such a way that makes it impossible for the active row to maintain its position (for example, if it was the fifth row down and there are no longer four rows before it). This special case, and others like it, would have to be handled manually in your code.
A final issue is that behavior can become inconsistent based on how the user is adding or deleting rows. A newly added row normally becomes the active row and users will have to click on a row to delete it, which would cause that row to become active. In this situation, the active row would become another row in the grid and users may not understand why the grid is being scrolled automatically after deleting a row.
May I ask why you need to maintain the scroll position of the active row after making changes to the datasource? If possible, I will try to recommend a different approach.
Thanks for sending your source. I'm currently looking into how you can achieve this and I plan to build a sample for you.
I should have the sample ready by the end of the day on Thursday. Please let me know if you have any questions.