예제 #1
0
        public static void setGridviewDataSource(DataGridView grid, bool rememberRowPosition, bool reapplySort, object data)
        {
            //save top row index
            int topRowIndex      = grid.FirstDisplayedScrollingRowIndex;
            int selectedRowIndex = -1;

            if (grid.Rows.Count > 0 && grid.SelectedRows.Count > 0)
            {
                selectedRowIndex = grid.SelectedRows[0].Index;
            }

            //save sorting
            DataGridViewColumn sortColumn = grid.SortedColumn;
            ListSortDirection  sortOrder  = ListSortDirection.Ascending;

            if (grid.SortOrder == SortOrder.Descending)
            {
                sortOrder = ListSortDirection.Descending;
            }

            //update datasource
            grid.DataSource = data;

            //reapply sorting
            if (reapplySort && sortColumn != null)
            {
                grid.Sort(sortColumn, sortOrder);
            }

            //display top row index
            if (rememberRowPosition)
            {
                Tools.setFirstDisplayedScrollingRowIndex(grid, topRowIndex, selectedRowIndex);
            }
        }