c #datagridview行索引验证

时间:2015-03-17 18:39:06

标签: c# winforms datagridview

如果我双击gridview的行标题单元格,则会出现索引参数错误。我尝试使用我的RowIndex语句在双击事件中修复它,但是我还有其他地方应该这样做吗?

private void DGV1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {


                try
                {
                     if ((e.ColumnIndex > 0))
                    {
                        EditUser eu = new EditUser();
                        eu.UserId = DGV1.Rows[e.RowIndex].Cells[1].Value.ToString();
                        FormFunctions.OpenMdiDataForm(Program.GetMainMdiParent(), eu);    
                    }
                    if (e.RowIndex == 0 || e.RowIndex == -1)
                    {
                        return;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

1 个答案:

答案 0 :(得分:0)

您的验证逻辑未按所需顺序应用。将检查条件更改为以下内容:

try
{
    if (e.ColumnIndex > 0 && e.RowIndex > 0)
    {
        EditUser eu = new EditUser();
        eu.UserId = DGV1.Rows[e.RowIndex].Cells[1].Value.ToString();
        FormFunctions.OpenMdiDataForm(Program.GetMainMdiParent(), eu);    
    }
}
catch (Exception ex)
{
    MessageBox.Show("Error\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}