2011-11-14 25 views
0

我需要在第一次加載表單時選擇DataGridView中的最後一行。所以 我事件寫了這個代碼:當我使用c加載表單時選擇DataGridView中的最後一行#

da.SelectCommand = new SqlCommand("SELECT * FROM Days", cn); 

ds.Clear(); 
da.Fill(ds); 

dataGridView1.DataSource = dsD.Tables[0]; 
dataGridView1.ClearSelection(); 
int nRowIndex = dataGridView1.Rows.Count - 1; 
dataGridView1.Rows[nRowIndex].Selected = true; 
dataGridView1.FirstDisplayedScrollingRowIndex = nRowIndex; 
dataGridView1.Refresh(); 

但這isn't做任何事情。它仍然選擇第一行,但是當我將數據保存在數據庫中然後填充數據集時,此代碼將起作用。但爲什麼不是我第一次加載表單。

回答

0

我用:

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

和它的工作。

相關問題