My winforms skills are a bit rusty. I'm using a BindingSource
for a DataGridView
. On KeyDown
of the DataGridView
i want to select the next/previous record which works as desired.
I want to select the first if the user hits Keys.Down
when the last item is selected and select the last if he hits Keys.Up
when the first item is selected. But nothing happens then.
Here's the code:
private void Grid_Keydown(Object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
previousItem();
else if (e.KeyCode == Keys.Down)
nextItem();
}
private void previousItem()
{
BindingSource bs = null;
switch (this.Type) // a custom enum
{
case AdminType.Channel:
bs = channelBindingSource;
break;
default:
break;
}
if (bs.Position - 1 < 0)
bs.MoveLast();
else
bs.MovePrevious();
}
private void nextItem()
{
BindingSource bs = null;
switch (this.Type)
{
case AdminType.Channel:
bs = channelBindingSource;
break;
default:
break;
}
if (bs.Position + 1 >= bs.Count)
bs.MoveFirst();
else
bs.MoveNext();
}
Note that bs.MoveFirst()
/bs.MoveLast()
are called correctly but nothing happens.
Edit: Interesting, it works as expected when i trigger this from a button(previous/next) instead of the DataGridView
's OnKeyDown
, any ideas?