void UpdateGrids() { try { int scrollPosition = 0; int rowPositionsGrid = 0, rowWatchList = 0; //Getting sort order var propertyInfo = typeof(Stock).GetProperty(stocks.sortProperty); List <Stock> sortedPositions = stocks.OrderBy(x => x.NoOfShares).ToList(); List <Stock> sortedWatch = stocks.OrderBy(x => x.NoOfShares).ToList(); //Saving selected row index if (PositionsGrid.Rows.Count > 0 && PositionsGrid.SelectedRows != null && PositionsGrid.SelectedRows.Count > 0) { rowPositionsGrid = PositionsGrid.SelectedRows[0].Index; } if (WatchListGrid.Rows.Count > 0 && WatchListGrid.SelectedRows != null && WatchListGrid.SelectedRows.Count > 0) { rowWatchList = WatchListGrid.SelectedRows[0].Index; } //if list is sorted ascending if (stocks.sortDirection == ListSortDirection.Ascending) { scrollPosition = PositionsGrid.FirstDisplayedScrollingRowIndex; PositionsGrid.DataSource = new ThreadedBindingList <Stock>(sortedPositions.Where(s => s.NoOfShares > 0) .OrderBy(x => propertyInfo.GetValue(x, null)) .ToList()); if (scrollPosition > 0) { PositionsGrid.FirstDisplayedScrollingRowIndex = scrollPosition; } scrollPosition = WatchListGrid.FirstDisplayedScrollingRowIndex; WatchListGrid.DataSource = new ThreadedBindingList <Stock>(sortedWatch.Where(s => s.NoOfShares == 0) .OrderBy(x => propertyInfo.GetValue(x, null)) .ToList()); if (scrollPosition > 0) { WatchListGrid.FirstDisplayedScrollingRowIndex = scrollPosition; } } else //if list is sorted desending { scrollPosition = PositionsGrid.FirstDisplayedScrollingRowIndex; PositionsGrid.DataSource = new ThreadedBindingList <Stock>(sortedPositions.Where(s => s.NoOfShares > 0) .OrderByDescending(x => propertyInfo.GetValue(x, null)) .ToList()); if (scrollPosition > 0) { PositionsGrid.FirstDisplayedScrollingRowIndex = scrollPosition; } scrollPosition = WatchListGrid.FirstDisplayedScrollingRowIndex; WatchListGrid.DataSource = new ThreadedBindingList <Stock>(sortedWatch.Where(s => s.NoOfShares == 0) .OrderByDescending(x => propertyInfo.GetValue(x, null)) .ToList()); WatchListGrid.DataSource = new ThreadedBindingList <Stock>(sortedWatch.Where(s => s.NoOfShares == 0) .OrderBy(x => propertyInfo.GetValue(x, null)) .ToList()); if (scrollPosition > 0) { WatchListGrid.FirstDisplayedScrollingRowIndex = scrollPosition; } } if (rowPositionsGrid != 0 && rowPositionsGrid < PositionsGrid.Rows.Count) { PositionsGrid.Rows[rowPositionsGrid].Selected = true; } if (rowWatchList != 0 && rowWatchList < WatchListGrid.Rows.Count) { WatchListGrid.Rows[rowWatchList].Selected = true; } } catch { } }