Ho qualche problema con l'impostazione dell'ultima riga nella mia vista datagrid selezionata. Seleziono l'ultima riga in questo modo:DatagridView Selezionare l'ultima riga
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)
{ }
}
Quando eseguo questo codice ottengo un'eccezione: IndexOutOfRangeException occurred
: Indice-1 non ha un valore.
Quando eseguo il debug della raccolta Rows
e della corrispondente collezione Cells
, vedo che entrambe le raccolte sono riempite. L'indice esiste anche della collezione Righe e celle.
Non ho idea di cosa sto facendo male qui. Qualcuno che può aiutarmi qui? Thnx
EDIT:
Qui è l'eccezione completo:
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)
codice sembra che vada bene. Hai provato a debuggarlo? Su quale riga di codice ottieni l'errore? –
Prova un ** Quick Watch ** per 'grid.Rows [grid.Rows.Count - 1] .Cells [1]' e guarda cosa restituisce. –
@Aseem: la riga è: 'grid.CurrentCell = grid.Rows [grid.Rows.Count - 1] .Cells [1]' – Martijn