Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 4.0 使用c加载表单时,选择DataGridView中的最后一行#_C# 4.0_Datagridview_Selection - Fatal编程技术网

C# 4.0 使用c加载表单时,选择DataGridView中的最后一行#

C# 4.0 使用c加载表单时,选择DataGridView中的最后一行#,c#-4.0,datagridview,selection,C# 4.0,Datagridview,Selection,当窗体第一次加载时,我需要选择DataGridView中的最后一行。所以 我在Form\u Load事件中编写了此代码: da.SelectCommand = new SqlCommand("SELECT * FROM Days", cn); ds.Clear(); da.Fill(ds); dataGridView1.DataSource = dsD.Tables[0]; dataGridView1.ClearSelection(); int nRowIndex = dataGridVie

当窗体第一次加载时,我需要选择
DataGridView
中的最后一行。所以 我在
Form\u Load
事件中编写了此代码:

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();
但这没有任何作用。它仍然选择第一行,但是当我将数据保存在数据库中,然后填充数据集时,这段代码会起作用。但是为什么不在我第一次加载表单时使用呢。

我使用了:

dataGridView1.CurrentCell = dataGridView1.Rows[nRowIndex].Cells[0];
它成功了