![](/img/trans.png)
[英]Able to select a row but shouldn't select the last column in that row in a DataGridView
[英]DatagridView Select last row
我在设置 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 集合中。
我不知道我在这里做错了什么。 有人可以帮助我吗? 谢谢
编辑:
这是完整的例外:
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)
尝试:
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;
提供以下输出:(最后一行,滚动并选择)
我知道这可能有点晚了,但它可能对其他人有用。
你有没有试过这个:
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(); ;)
真诚的
你有没有想过为此使用 Linq?
grid.Rows.OfType<DataGridViewRow>().Last().Selected = true;
grid.CurrentCell = grid.Rows.OfType<DataGridViewRow>().Last().Cells.OfType<DataGridViewCell>().First(); // if first wanted
IndexOutOfRangeException 的“catch”块是空的,根本不会显示任何错误。
要么您的问题不准确,要么在其他地方抛出异常。
编辑:查看您添加的调用堆栈后,我可以看到错误确实不是在这里抛出,而是在CurrencyManager
类的Current
/ Item
属性中,最终由调用CurrentCell
设置器触发.
底线:问题不在这段代码中; 异常是由您设置当前单元格触发的其他一些代码引发的。
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Selected = true;
最后一行是空的,因为它是用户可以添加新行的空行。 无法选择此行。 你试过 grid.Rows.Count - 2 吗? 作为替代方法,您可以将 AllowUserToAddRows 设置为 false。
为避免 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;
这很容易。 使用:dataGridView.CurrentCell=dataGridView[ColumnIndex, RowIndex];
dataGridView[X,y]...
而不是
dataGridView[Y, X]...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.