2011-06-30 47 views
3

我在Windows窗體中有一個DataGridView,並且此網格包含接近實時數據 - 我希望它每隔20秒更新一次。我的網格綁定到我編程生成的DataTable,所以我現在有一個計時器生成此DataTable並將其每20秒分配給一個網格。在DataGridView中優雅刷新數據

但我的問題是,當數據刷新時,網格上的任何選擇或排序消失,這是一個很大的可用性問題。是否有其他方式來刷新網格的內容,保留選擇/排序?

回答

1

您需要保存您的選擇,然後重新載入數據並重新應用保存的選擇。

這實際上是一個相當簡單的過程,但我沒有在Winforms中編寫代碼來給出一個完整的例子,但程序會類似於;

通過DataGridViewItems循環。存儲索引和/或密鑰和選擇標準。

重新加載數據。

通過DataGridViewItems循環。檢索存儲的選擇標準,其中索引/鍵匹配並應用。

0

爲什麼你經常刷新數據?有很多改變嗎?插入?

如果插入更新網格只有新記錄就足夠了,通過將它們附加到網格的行集合。

2

恢復排序順序和行選擇的粗略指南。

刷新之前:

// Store the sort details. 
ListSortDirection oldSortOrder; 
switch (uiGrid.SortOrder) 
{ 
    case SortOrder.Ascending: 
     oldSortOrder = ListSortDirection.Ascending; 
     break; 
    case SortOrder.Descending: 
     oldSortOrder = ListSortDirection.Descending; 
     break; 
    default: 
     oldSortOrder = ListSortDirection.Ascending; 
     break; 
} 

DataGridViewColumn oldSortColumn = uiGrid.SortedColumn; 

// Store the selected rows 
List<String> selectedRows = new List<String>(); 
foreach (DataGridViewRow row in uiGrid.SelectedRows) 
{ 
    selectedRows.Add(row.Cells["SomeIndexColumn"].Value.ToString()); 
} 

刷新之後:

// Restore the sort 
uiGrid.Sort(oldSortColumn, oldSortOrder); 

// Restore Selected rows 
foreach (DataGridViewRow row in uiGrid.SelectedRows) 
{ 

    if (selectedRows.Contains(row.Cells["SomeIndexColumn"].Value.ToString())) 
    { 
     row.Selected = true; 
    } 
} 
0

我喜歡馬丁的答案。然而,我發現在刷新數據源之後,變量oldSortColumn以某種方式改變,並且數據gridview在「uiGrid.Sort(oldSortColumn,oldSortOrder)」中不接受它。此外,uiGrid.Sort不會將oldSortColumn視爲null。我很感激,如果有人能讓我知道爲什麼oldSortColumn改變。我決定使用排序列索引而不是排序列本身。因此,這裏是我的稍作修改的版本:

public static void GetDataGridViewUIInfo(this DataGridView dgv, out List<int> selectedIndices, 
    out int? sortColumnIndex, out ListSortDirection sortOrder) 
{ 
    selectedIndices = dgv.GetSelectedRowIndices(); 
    dgv.GetSortInfo(out sortColumnIndex, out sortOrder); 
} 

static List<int> GetSelectedRowIndices(this DataGridView dgv) 
{ 
    List<int> selectedIndices = new List<int>(); 

    foreach (DataGridViewRow row in dgv.SelectedRows) 
    { 
    selectedIndices.Add(row.Index); 
    } 

    return selectedIndices; 
} 

static void GetSortInfo(this DataGridView dgv, out int? sortColumnIndex, out ListSortDirection sortOrder) 
{ 
    // Store the sort details 
    switch (dgv.SortOrder) 
    { 
    case SortOrder.Ascending: 
     sortOrder = ListSortDirection.Ascending; 
     break; 
    case SortOrder.Descending: 
     sortOrder = ListSortDirection.Descending; 
     break; 
    default: 
     sortOrder = ListSortDirection.Ascending; 
     break; 
    } 

    sortColumnIndex = dgv.SortedColumn == null ? null : (int?)dgv.SortedColumn.Index; 
} 

刷新之後::

public static void SetDataGridViewUIInfo(this DataGridView dgv, List<int> selectedIndices, 
    int? sortColumnIndex, ListSortDirection sortOrder) 
{ 
    dgv.SetSelectedRowIndices(selectedIndices); 
    dgv.SetSortInfo(sortColumnIndex, sortOrder); 
} 

static void SetSelectedRowIndices(this DataGridView dgv, List<int> selectedIndices) 
{ 
    if (dgv.Rows.Count <= 0) 
    // Early out if there is no row in the data grid view 
    return; 

    foreach (DataGridViewRow row in dgv.Rows) 
    { 
    row.Selected = false; 
    } 

    foreach (int index in selectedIndices) 
    { 
    if (index < dgv.Rows.Count) 
     dgv.Rows[index].Selected = true; 
    else 
     dgv.Rows[dgv.Rows.Count - 1].Selected = true; 
    } 

    if (selectedIndices.Count > 0 && selectedIndices[0] < dgv.Rows.Count && dgv.DataSource is BindingSource) 
    ((BindingSource)dgv.DataSource).Position = selectedIndices[0]; 
} 

static void SetSortInfo(this DataGridView dgv, int? sortColumnIndex, ListSortDirection sortOrder) 
{ 
    if (sortColumnIndex == null) 
    // Early out if there was no column used to sort in the data grid view 
    return; 

    // Restore the sort details 
    dgv.Sort(dgv.Columns[(int)sortColumnIndex], sortOrder); 
} 

使用此調用上述功能:

List<int> selectedIndices; 
ListSortDirection sortOrder; 
int? sortColumnIndex; 
DataGridView dgv; 

dgv.GetDataGridViewUIInfo(out selectedIndices, out sortColumnIndex, out sortOrder); 

// Refresh the data source of the data gridview 

dgv.SetDataGridViewUIInfo(selectedIndices, sortColumnIndex, sortOrder); 

刷新之前執行此操作