我在 vb.net 中有一个我想在 C# 中使用的代码。代码是:
Dim cell As DataGridViewImageCell = CType(tempGrid.Rows(e.RowIndex).Cells(e.ColumnIndex), DataGridViewImageCell)
我正在尝试获取相应的 C# 代码:
public void gridmouseclick(object sender, MouseEventArgs e)
{
int i;
DataGridViewCell cell;
for (i = 0; i <= 1 - 1; i++)
{
cell = (DataGridViewCell)grid[i].Rows[e.X].Cells[e.Y];
if (e.Button == MouseButtons.Left)
{
cell.Value = imglst.Images[1];
}
else if (e.Button == MouseButtons.Right)
{
cell.Value = imglst.Images[0];
}
}
}
imglst
是的ImageList
,所以现在当我单击网格单元格时出现异常,异常是:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
我正在分配gridmouseclick
这样的......
grid[i].CellMouseClick += new DataGridViewCellMouseEventHandler(this.gridmouseclick);
我该如何摆脱这个异常?