【发布时间】:2012-11-23 17:56:55
【问题描述】:
直到昨天,以下代码工作正常,但今天我收到一条错误消息 Argument out of exception & index out of range。
我要在这里做什么,从最后一行的index 3(4th col) 获取该单元格值并放入col 3 (index 2) cell。当我在最后一个单元格(从下到上)键入它时,我收到了上述错误消息。
请帮帮我。
private void datagridview_CellValidated(object sender, CellValidatedEventArgs e)
{
if (e.ColumnIndex != 3)
return;
int nextRowIndex = e.RowIndex -1;
int lastRowIndex = datagridview.Rows.Count;
try
{
if (nextRowIndex <= lastRowIndex)
{
var valuesForcell = datagridview.Rows[e.RowIndex].Cells[3].Value.ToString();
datagridview.Rows[nextRowIndex].Cells[2].Value = valuesForcell;
datagridview.Rows[nextRowIndex].Cells[2].ReadOnly = true;
datagridview.Rows[nextRowIndex].Cells[2].Style.ForeColor = Color.MediumVioletRed;
datagridview.ClearSelection();
datagridview.SelectionMode = GridViewSelectionMode.CellSelect;
datagridview.Rows[nextRowIndex].Cells[3].BeginEdit();
}
}
catch (Exception exception) { }
}
【问题讨论】:
-
代替
nextRowIndex <= lastRowIndex试试nextRowIndex < lastRowIndex -
设置索引为 0 的单元格值时会发生什么?在您的示例中,您在其中设置了
45 -
我从 col 3 的最后一个单元格开始,然后 col 4 的最后一个单元格,然后向上直到 col 4 中最后一个单元格的末尾(从下到上)
-
@linguini check my answer
-
您的表格中有标题吗?
RowIndex对应于第一个数据行(45所在的位置)?
标签: c# winforms datagridview