我在设置我选择的datagridview中的最后一行时遇到了一些麻烦。我选择最后一行是这样的:DatagridView选择最后一行
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
:指数-1不具有价值。
当我调试Rows
集合和相应的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)
代码似乎很好。你尝试过调试吗?你在哪行代码中得到错误? – 2010-09-28 08:08:34
为'grid.Rows [grid.Rows.Count - 1] .Cells [1]'尝试**快速监视**并查看它返回的内容。 – 2010-09-28 08:08:49
@Aseem:行是:'grid.CurrentCell = grid.Rows [grid.Rows.Count - 1] .Cells [1]' – Martijn 2010-09-28 08:21:32