English 中文(简体)
选择数据组中的最后一行 当我用c#装上表格时的看法
原标题:select the last row in DataGridView when I load the form using c#

I need to select the last row in DataGridView, when form loads the first time. So I wrote this code in Form_Load event :

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();

But this isn´t doing anything. It still selects the first row, but this code works when I save the data in the database and then fill the dataset. But why not the first time when I load the form.

问题回答

I used :

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

它发挥了作用。





相关问题
DataGridView / Enter Key?

I m using vb.net 2008 and DataGridView. I m looking for code that will allow me to have the enter key move to the next column to the right instead of moving down one row while remaining in the same ...

Disable Cell Highlighting in a datagridview

How to disable Cell Highlighting in a datagridview, Highlighting should not happen even if I click on the cell. Any thoughts please

Saving nested XML in C#

I ve go the following XML file... <A> <B> <C>ValueX</C> <D>ValueY</D> </B> </A> Which I read into a DataSet to display it in a ...

How to sort databound DataGridView column?

I know that there are a lot of questions on this topic. I have been through all of them but nothing seems to help. How to sort by clicking on column header? How should I modify this code to do the ...

热门标签