2010-09-28 244 views
7

我在设置我选择的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) 
+0

代码似乎很好。你尝试过调试吗?你在哪行代码中得到错误? – 2010-09-28 08:08:34

+0

为'grid.Rows [grid.Rows.Count - 1] .Cells [1]'尝试**快速监视**并查看它返回的内容。 – 2010-09-28 08:08:49

+0

@Aseem:行是:'grid.CurrentCell = grid.Rows [grid.Rows.Count - 1] .Cells [1]' – Martijn 2010-09-28 08:21:32

回答

1

为IndexOutOfRangeException你 '抓' 块是空的,并不会在所有显示任何错误。

要么你的问题不是确切的,要么就是抛出异常。

编辑:已经通过自己添加的,我可以看到错误确实是你的调用堆栈,在这里被抛出,而是在CurrencyManager类的Current/Item特性,它最终是看通过调用CurrentCell setter来触发。底线:这个问题不在这个代码中;这个问题不在这个代码中。由设置当前单元触发的其他代码引发异常。

+0

它在调试模式下抛出excetpion。代码停止运行,VS突出显示。但这不是重点。关键是,WHY是抛出的异常? – Martijn 2010-09-28 08:05:32

+0

我会使用“立即”窗口确认grid.Rows.Count的值,然后尝试访问grid.Rows [0]或使用整数字面值的任何数字,以确保。我怀疑这里的代码并不是导致异常的代码的精确副本,而在你和你认为的代码之间的某个地方,你的代码正在做这个错误。 – 2010-09-28 08:11:05

+0

在即时窗口中,我有收藏中的行和单元格 – Martijn 2010-09-28 08:17:35

2

你有没有想过为此使用Linq?

grid.Rows.OfType<DataGridViewRow>().Last().Selected = true; 
    grid.CurrentCell = grid.Rows.OfType<DataGridViewRow>().Last().Cells.OfType<DataGridViewCell>().First(); // if first wanted 
+0

不,我没有。但是我没有扩展方法'Last()'和'First()'。我有使用System.Linq – Martijn 2010-09-28 08:12:38

+0

对不起,我编辑纠正linq现在 – danijels 2010-09-28 08:20:46

+0

谢谢。试过你的解决方案,但我在这一行上得到相同的异常:'grid.CurrentCell = grid.Rows.OfType ()。Last().Cells.OfType ()。Last();' – Martijn 2010-09-28 08:24:57

-1

最后一行是空的,因为它是用户可以添加新行的空行。这一行不能被选择。 您是否尝试过grid.Rows.Count - 2?作为替代,您可以将AllowUserToAddRows设置为false。

+0

我试过你的解决方案,但它没有奏效。 – Martijn 2010-09-28 08:20:53

+0

我已经将该属性设置为false .. – Martijn 2010-09-28 08:28:37

+0

-1 - 这就是所谓的抓着吸管,而不是调试。 – 2010-09-28 08:41:50

11

尝试:

dataGridView1.ClearSelection();//If you want 

int nRowIndex = dataGridView1.Rows.Count - 1; 
int nColumnIndex = 3; 

dataGridView1.Rows[nRowIndex].Selected = true; 
dataGridView1.Rows[nRowIndex].Cells[nColumnIndex].Selected = true; 

//In case if you want to scroll down as well. 
dataGridView1.FirstDisplayedScrollingRowIndex = nRowIndex; 

给出了下面的输出:(最后一排,滚动和选择)

alt text

+0

我试过这个,但没有奏效。 – Martijn 2010-09-28 09:17:10

+0

@Martijn:我在发布之前测试了代码。什么不工作? – 2010-09-28 09:26:50

+0

上线'dataGridView1.Rows [nRowIndex] .Cells [nColumnIndex] .Selected = true; '我得到了令人讨厌的异常 – Martijn 2010-09-28 09:52:12

4

我知道这可能是晚了一点,但也可能是使用的给别人。

你有没有尝试过这样的:

grid.Rows.Row[grid.Rows.Count -1].Selected = true;

在我的Windows应用程序,我第一次使用你的代码在我的DataGridView,我得到了同样的异常..然后它在晚上来找我,当我在我的床(我是编程的新手)。

如果我写为:Rows[Rows.count-1]第一行是“0”和“0-1 = -1”,所以它超出范围:)

然后当我改变了我的代码Rows.Row[index]它的工作! :)


如果您使用c#3.0和更高版本的替代方法:检出CellAddress(); ;)

真诚