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.