Tengo algunos problemas para configurar la última fila en mi vista de cuadro de datos seleccionado. Selecciono la última fila de esta manera:DatagridView Seleccionar la última fila
if (grid.Rows.Count > 0)
{
try
{
grid.Rows[grid.Rows.Count - 1].Selected = true;
grid.CurrentCell = grid.Rows[grid.Rows.Count - 1].Cells[1]
}
catch (IndexOutOfRangeException)
{ }
catch (ArgumentOutOfRangeException)
{ }
}
Cuando ejecuto el código me sale una excepción: IndexOutOfRangeException occurred
: Índice-1 no tiene un valor.
Cuando depuro la colección Rows
y la correspondiente colección Cells
veo que se llenan ambas colecciones. El índice también existe de la colección Filas y Células.
No tengo ni idea de qué estoy haciendo mal aquí. ¿Alguien que pueda ayudarme aquí? Thnx
EDIT:
Aquí es la excepción completa:
System.IndexOutOfRangeException: Index -1 does not have a value.
at System.Windows.Forms.CurrencyManager.get_Item(Int32 index)
at System.Windows.Forms.CurrencyManager.get_Current()
at System.Windows.Forms.DataGridView.DataGridViewDataConnection.OnRowEnter(DataGridViewCellEventArgs e)
at System.Windows.Forms.DataGridView.OnRowEnter(DataGridViewCell& dataGridViewCell, Int32 columnIndex, Int32 rowIndex, Boolean canCreateNewRow, Boolean validationFailureOccurred)
at System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32 columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean throughMouseClick)
at System.Windows.Forms.DataGridView.set_CurrentCell(DataGridViewCell value)
El código parece correcto. ¿Has probado a depurarlo? ¿En qué línea de código está obteniendo el error? –
Pruebe un ** Quick Watch ** para 'grid.Rows [grid.Rows.Count - 1] .Cells [1]' y vea lo que devuelve. –
@Aseem: la línea es: 'grid.CurrentCell = grid.Rows [grid.Rows.Count - 1] .Cells [1]' – Martijn