I'm having a gridEX component and up/down buttons for changing the selected row accordingly. If I select a certain row from the table, the up button should select the row above the previously selected row.
private void btnUp_Click(object sender, EventArgs e)
{
//TODO
int rowIndex = gridEX.Row;
if (rowIndex > 0)
{
GridEXRow newSelectedRow = gridEX.GetRow(rowIndex-1);
gridEX.SelectedItems.Clear();
gridEX.MoveTo(newSelectedRow);
}
}
The code above selects the right row, but the selection is not visible, like it would be if I click on the row. What could be the problem?
Clicking on the up/down buttons causes the grid to lose focus. This is why the selected row is not highlighted. You need to set focus back to the grid before changing rows. Something like this:
private void btnUp_Click(object sender, EventArgs e)
{
int rowIndex = gridEX1.CurrentRow.RowIndex - 1;
selectRow(rowIndex);
}
private void btnDown_Click(object sender, EventArgs e)
{
int rowIndex = gridEX1.CurrentRow.RowIndex + 1;
selectRow(rowIndex);
}
private void selectRow(int rowIndex)
{
gridEX1.Focus(); //set the focus back on your grid here
if (rowIndex >= 0 && rowIndex < (gridEX1.RowCount))
{
GridEXRow newSelectedRow = gridEX1.GetRow(rowIndex);
gridEX1.MoveToRowIndex(rowIndex);
}
}