c#datagridview行独立验证



如果双击网格视图的行标题单元格,则会出现索引参数错误。我试着用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("Errorn" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

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

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("Errorn" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

最新更新