How to set the black arrow at the right row in a DataGridView?

D

david.vincent

During the initialization, I set the current selected row to the first row
of my DataGridView.
But the black arrow of the first column (what is the aim of this arrow?) is
set at a random row, not the same row that my selected one.
How to force the same row at the initialization time? (after the first
manual selection, it's ok)

Thanks for your help,
Jean-Michel
 
M

Morten Wennevik [C# MVP]

Hi Jean-Michel,

You are probably setting a certain row as selected, but the black triangle cursor follows the CurrentRow property. The CurrentRow property is aread only property that follows CurrentCell, which you can set.

Try setting the CurrentCell property to a cell in the desired row.

dataGridView1.CurrentCell = dataGridView1.Rows[2].Cells[0];
 
D

david.vincent

Thanks Wenning.
I always get an error "Current cell cannot be set to an invisible cell.",
both with CurrentCell and FirstDisplayedCell, even with grids with no
invisible column.
Is there a test to perform before?
 
M

Morten Wennevik [C# MVP]

Thanks Wenning.
I always get an error "Current cell cannot be set to an invisible cell..",
both with CurrentCell and FirstDisplayedCell, even with grids with no
invisible column.
Is there a test to perform before?

Well I don't know what your grid looks like, but you can test for visibility before assigning the CurrentCell

DataGridViewCell cell = dataGridView1.Rows[2].Cells[0];
if(cell.Visible)
dataGridView1.CurrentCell = dataGridView1.Rows[2].Cells[0];

You may also need to ensure the data is fed to the grid before trying toassign the CurrentCell. In my sample I filled the DataGridView in the form constructor and set the CurrentCell property in the Form.Load event, however my code worked fine if I set the CurrentCell in the constructor as well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top