У меня возникли проблемы с настройкой последней строки в моем выбранном сетевом представлении.Я выбираю последнюю строку следующим образом:
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)
{ }
}
Когда я выполняю этот код, я получаю исключение: IndexOutOfRangeException occurred
: у Index-1 нет значения.
Когда я отлаживаюRows
коллекция и соответствующая Cells
коллекция, которую я вижу, обе коллекции заполнены.Существует также индекс коллекции Rows and Cells.
Я понятия не имею, что я делаю здесь неправильно.Кто-то, кто может помочь мне здесь?Thnx
РЕДАКТИРОВАТЬ:
Вот полное исключение:
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)