Mahdi Tahsildari
Mahdi Tahsildari

Reputation: 13582

Show DataGridView's first row (scroll up to first row)

In my DataGridView control I want to go to first row when user hits Home button.

I caught the keyDown event and here's the code:

if (e.KeyCode == Keys.Home)
{
    dgvMain.FirstDisplayedCell = dgvMain.Rows[0].Cells[0];
}

does not work, nor does this one :

if (e.KeyCode == Keys.Home)
{
    dgvMain.FirstDisplayedScrollingRowIndex = dgvMain.Rows[0].Index;
}

these codes make grid scroll up a bit but not to the first row.

How can I focus on first Row? Without selecting it.

Upvotes: 0

Views: 3562

Answers (1)

Mahdi Tahsildari
Mahdi Tahsildari

Reputation: 13582

I found were the problem was. I still don't understand why, but handling the event made it work:

if (e.KeyCode == Keys.Home)
{
    e.Handled = true;
    dgvMain.FirstDisplayedScrollingRowIndex = 0;
}

Home does not do anything (AFAIK) but when I handle it everything is ok. Why? I don't know.

Upvotes: 1

Related Questions