2010-03-14 30 views
13

後的位置,這是我的代碼:如何保存重裝的DataGridView

 private void getData(string selectCommand) 
    { 
     string connectionString = @"Server=localhost;User=SYSDBA;Password=masterkey;Database=C:\data\test.fdb"; 

     dataAdapter = new FbDataAdapter(selectCommand, connectionString); 
     DataTable data = new DataTable(); 
     dataAdapter.Fill(data); 
     bindingSource.DataSource = data; 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     getData(dataAdapter.SelectCommand.CommandText); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     dataGridView1.DataSource = bindingSource; 
     getData("SELECT * FROM cities"); 
    } 

在Button1的Click重裝數據後,小區選擇跳到上第一列和滾動條復位。 如何保存DataGridView的位置?

回答

3

你可以保存選定行推出的getData,使用前DataGridView.CurrentRow,並選擇該行的網格已被加載後。

this question我回答瞭如何在一個DataGridView選擇一個特定的行。


編輯:我假設你正在使用的WinForms

EDIT2:又是怎麼回事滾動條?

您可以保存第一個可見行的索引,也有這種說法

DataGridView.FirstDisplayedCell.RowIndex 
0

目前,正在加載的頁面加載數據每次。我會建議使用Page.IsPostback屬性來檢查它是否是回發。

if(!Page.IsPostback) 
{ 
    dataGridView1.DataSource = bindingSource; 
    getData("SELECT * FROM cities"); 
} 

這會減少數據庫上的負載量,並且會退出導致您選擇的問題。

+0

此Page.IsPostback屬性僅對ASP.NET應用程序有效。我們在這裏談論WinForms。 – 2010-03-14 15:10:40

0

這這樣做,因爲你重置或reaffecting您DataGridView控件的DataSource屬性。

爲了執行你想要的,你必須在重置DataGridView的DataSource屬性之前將CurrentItem索引保存到本地變量中。然而,這樣做意味着你知道你將擁有相同或更多的數據量,否則你會得到一個IndexOutOfRangeException試圖移動到一個更大的索引,而不是實際包含在你的DataGridView中的數據量。

所以,如果你想保存你的行或單元格的索引,則需要通過DataGridView控件屬性,從而爲您的BindingSource西港島線不能提供這樣的功能。

在一個DataGridView,所選行 和當前行(通過行標題的 箭頭指示)可能不是 同一行。另外,我們可以在DataGridView中選擇多行 ,但當前行只能是一行 行。當DataGridView中的SelectionMode屬性 設置爲 FullRowSelect,當前行會 總是選擇。如果您想在 DataGridView控件 改變當前行,你可以通過設置 CurrentCell財產

dataGridView1.CurrentCell = dataGridView1.Rows[1].Cells[0]; 

如果您想只改變所選行,你可以通過設置選擇你想要的行的屬性爲true。

dataGridView1.CurrentRow.Selected = false; 
dataGridView1.Rows[1].Selected = true; 
+0

什麼是滾動條?如果用戶只是滾動數據網格而不選擇單元格。 – bobik 2010-03-14 15:43:29

+0

然後,DataGridView和BindingSource都不能識別用戶正在滾動的位置。當您加載數據並將其設置爲DataGridView的DataSource屬性時,您會告訴WinForm使用新數據進行刷新,然後重新初始化所有內容。我知道沒有其他方式,然後與選定的單元格或行。 Windows本身行爲不同。 – 2010-03-14 15:51:45

+0

@bobk,我在我的帖子中回答了這個問題,最後一次編輯。 – Javier 2010-03-14 15:52:40

1

在另一個論壇,我發現沒有任何操作的解決方案:

private void getData(string selectCommand) 
    { 
     string connectionString = @"Server=localhost;User=SYSDBA;Password=masterkey;Database=C:\data\test.fdb"; 

     dataAdapter = new FbDataAdapter(selectCommand, connectionString); 
     data = new DataTable(); 
     dataAdapter.Fill(data); 
     bindingSource.DataSource = data; 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     dataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey; 
     dataAdapter.Fill(data); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     dataGridView1.DataSource = bindingSource; 
     getData("SELECT * FROM cities"); 
    } 
31

這是我想出了一個解決方案。不需要選擇一行,並在刷新後將滾動條放回同一區域,條件是行數不會有很大差異。

int saveRow = 0; 
if (dataGridView1.Rows.Count > 0) 
    saveRow = dataGridView1.FirstDisplayedCell.RowIndex; 

dataGridView1.DataSource = dataTable1; 

if (saveRow != 0 && saveRow < dataGridView1.Rows.Count) 
    dataGridView1.FirstDisplayedScrollingRowIndex = saveRow; 
+0

正是我所需要的,看起來像我的答案:) – Jacco 2014-05-02 12:25:35

+0

@ovinophile如何實現與datagrid這個伴侶?不與datagridview? – 2016-09-30 10:54:56

+0

我試過這個,並把它放到一個100ms的計時器,其中包含我的datagridview從數據庫重新加載和這個代碼保存我在...我有一個空引用錯誤...我該如何解決這個問題? – 2017-12-11 20:59:58

4
int FirstDisplayedScrollingRowIndex = this.dgvItems.FirstDisplayedScrollingRowIndex; //Save Current Scroll Index 
int SelectedRowIndex = 0; 
if (this.dgvItems.SelectedRows.Count > 0) SelectedRowIndex = this.dgvItems.SelectedRows[0].Index; //Save Current Selected Row Index 

//REFRESH DataGridView HERE 

if ((FirstDisplayedScrollingRowIndex >=0) && ((this.dgvItems.Rows.Count -1) >= FirstDisplayedScrollingRowIndex)) this.dgvItems.FirstDisplayedScrollingRowIndex = FirstDisplayedScrollingRowIndex; //Restore Scroll Index 
if ((this.dgvItems.Rows.Count -1) >= SelectedRowIndex) this.dgvItems.Rows[SelectedRowIndex].Selected = true; //Restore Selected Row 
+0

我試過上面的答案的代碼,我把它放在一個100毫秒的計時器,它給了我一個空的參考錯誤在我的DataGridView ...如果我使用你的代碼格式,而不是空值參考錯誤被修復? – 2017-12-11 21:03:09

+0

空引用錯誤是非常通用的,很難說出什麼問題。這段代碼沒有被寫入在一個定時器內執行,所以這可能是你的問題。先嚐試沒有計時器,然後從那裏開始工作。 – 2017-12-12 22:34:14

+0

它沒有定時器,沒有空引用錯誤。有人告訴我也許是因爲100ms。定時器執行得太快,datagridview甚至沒有被調用。所以我試圖將計時器增加到1秒,並且啓動時的空引用錯誤消失了,但並不完全是因爲有時它會再次彈出空引用錯誤。 – 2017-12-12 22:59:08

3

簡單的方法是吹代碼:

int CurrentRowIndex = (hSuperGrid1.CurrentRow.Index); 

////after Fill The DataGridView 

hSuperGrid1.ClearSelection(); 
hSuperGrid1.CurrentRow.Selected = false; 

hSuperGrid1.Rows[CurrentRowIndex].Selected = true; 

hSuperGrid1.CurrentCell = hSuperGrid1[0, CurrentRowIndex]; 
2

@ ovinophile的回答幫助了,肯定的,但它並沒有解決在DataGridView的水平滾動我。在@ ovinophile的答案上捎帶,這可以很好地保持水平和垂直滾動位置:

// Remember the vertical scroll position of the DataGridView 
int saveVScroll = 0; 
if (DataGridView1.Rows.Count > 0) 
    saveVScroll = DataGridView1.FirstDisplayedCell.RowIndex; 

// Remember the horizontal scroll position of the DataGridView 
int saveHScroll = 0; 
if (DataGridView1.HorizontalScrollingOffset > 0) 
    saveHScroll = DataGridView1.HorizontalScrollingOffset; 

// Refresh the DataGridView 
DataGridView1.DataSource = ds.Tables(0); 

// Go back to the saved vertical scroll position if available 
if (saveVScroll != 0 && saveVScroll < DataGridView1.Rows.Count) 
    DataGridView1.FirstDisplayedScrollingRowIndex = saveVScroll; 

// Go back to the saved horizontal scroll position if available 
if (saveHScroll != 0) 
    DataGridView1.HorizontalScrollingOffset = saveHScroll; 
+0

我刷新我的DataGridView使用從我的數據庫執行一個SQL查詢的函數,而不是...如果我把我的重裝函數代替你的代碼格式的'DataGridView1.DataSource = ds.tables(0)'將它爲我工作呢? – 2017-12-11 21:05:53

相關問題