简体   繁体   English

DatagridView 选择最后一行

[英]DatagridView Select last row

I have some trouble with setting the last row in my datagridview selected.我在设置 datagridview 中选择的最后一行时遇到了一些麻烦。 I select the last row this way:我这样选择最后一行:

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)
    { }
}

When I execute this code I get an exception: IndexOutOfRangeException occurred : Index-1 does not have a value.当我执行此代码时,出现异常: IndexOutOfRangeException occurred :Index-1 没有值。

When I debug the Rows collection and the corresponding Cells collection I see both collections are filled.当我调试Rows集合和相应的Cells集合时,我看到两个集合都已填充。 The index also exists of the Rows and Cells collection.该索引也存在于 Rows 和 Cells 集合中。

I have no clue what I am doing wrong here.我不知道我在这里做错了什么。 Someone who can help me out here?有人可以帮助我吗? Thnx谢谢

EDIT:编辑:

Here is the complete exception:这是完整的例外:

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)

Try:尝试:

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;

Gives following output : (Last row, scrolled and selected)提供以下输出:(最后一行,滚动并选择)

替代文字

I know this may be a little late but it may be of use to someone else.我知道这可能有点晚了,但它可能对其他人有用。

Have you tried this :你有没有试过这个:

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

In my windows app I first used your code in my datagridview and I got the same exception.. and then it came to me at night when I was in my bed (I'm a newbie to programming).在我的 Windows 应用程序中,我首先在我的 datagridview 中使用了你的代码,但我遇到了同样的异常。

If I write as : Rows[Rows.count-1] the first row is "0" and "0-1 = -1" so its out of range :)如果我写为: Rows[Rows.count-1]第一行是“0”和“0-1 = -1”,所以它超出了范围:)

Then when I changed my code to Rows.Row[index] it worked!然后当我将代码更改为Rows.Row[index]它起作用了! :) :)


An alternative if you use c# 3.0 and higher: check out CellAddress();如果您使用 c# 3.0 及更高版本,另一种选择:查看 CellAddress(); ;) ;)

Sincerely真诚的

Have you thought about using Linq for this?你有没有想过为此使用 Linq?

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

Your 'catch' block for IndexOutOfRangeException is empty, and will not display any error at all. IndexOutOfRangeException 的“catch”块是空的,根本不会显示任何错误。

Either your question is not exact, or the exception is being thrown somewhere else.要么您的问题不准确,要么在其他地方抛出异常。

EDIT: Having looked through your call stack that you added, I can see that the error is, indeed, not being thrown here, but rather in the CurrencyManager class's Current / Item properties, which is ultimately being triggered by the call to the CurrentCell setter.编辑:查看您添加的调用堆栈后,我可以看到错误确实不是在这里抛出,而是在CurrencyManager类的Current / Item属性中,最终由调用CurrentCell设置器触发.

Bottom line: the problem is not in this code;底线:问题不在这段代码中; the exception is being thrown by some other piece of code triggered by your setting the current cell.异常是由您设置当前单元格触发的其他一些代码引发的。

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

Eventually the last row is empty because it's the empty row with which the user can add a new row.最后一行是空的,因为它是用户可以添加新行的空行。 This row cannot be selected.无法选择此行。 Have you tried grid.Rows.Count - 2?你试过 grid.Rows.Count - 2 吗? As an alternative, you can set AllowUserToAddRows to false.作为替代方法,您可以将 AllowUserToAddRows 设置为 false。

To avoid the IndexOutOfRangeException make sure only to select a visible row.为避免 IndexOutOfRangeException,请确保仅选择可见行。 I suggest:我建议:

// 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;

It's very easy.这很容易。 use: dataGridView.CurrentCell=dataGridView[ColumnIndex, RowIndex];使用:dataGridView.CurrentCell=dataGridView[ColumnIndex, RowIndex];
dataGridView[X,y]... dataGridView[X,y]...
instead of而不是
dataGridView[Y, X]... dataGridView[Y, X]...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM