DatagridView选择最后一行

时间:2010-09-28 07:53:44

标签: c# 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:Index-1没有值。

当我调试Rows集合和相应的Cells集合时,我看到两个集合都已填充。该索引还存在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)

8 个答案:

答案 0 :(得分:14)

尝试:

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

答案 1 :(得分: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(); ;)

此致

答案 2 :(得分:2)

您是否考虑过使用Linq?

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

答案 3 :(得分:1)

IndexOutOfRangeException的'catch'块为空,根本不会显示任何错误。

要么你的问题不准确,要么在其他地方抛出异常。

编辑:查看了您添加的调用堆栈,我可以看到错误确实是被抛出此处,而是放在{{ 1}}类的CurrencyManager / Current属性,最终由对Item setter的调用触发。

底线:问题不在此代码中;由您设置当前单元格触发的其他一些代码抛出了异常。

答案 4 :(得分:0)

dataGridView1.Rows[dataGridView1.Rows.Count - 1].Selected = true;

答案 5 :(得分:0)

为避免IndexOutOfRangeException,请确保仅选择可见行。 我建议:

// Find last visible row
DataGridViewRow row = dataGridView1.Rows.Cast<DataGridViewRow>().Where(r => r.Visible).Last(); 
// scroll to last row if necessary
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows.IndexOf(row);
// select row
row.Selected = true;

答案 6 :(得分:0)

这很容易。采用:     dataGridView.CurrentCell = dataGridView [ColumnIndex,RowIndex];
    dataGridView [X,y] ...
代替
    dataGridView [Y,X] ...

答案 7 :(得分:-1)

最后一行是空的,因为它是用户可以添加新行的空行。无法选择此行。 你试过grid.Rows.Count - 2吗?作为替代方法,您可以将AllowUserToAddRows设置为false。

相关问题