System.IndexOutOfRangeException 错误在 winform 中单击 DataGridView

本文关键字:单击 DataGridView winform IndexOutOfRangeException 错误 System | 更新日期: 2023-09-27 17:57:15

我有一个名为dataGridView1的DataGridView,其中我制作了9列用于获取员工的详细信息,并为EditLinkCellColumn和DeleteLinkCellColumn制作了2列,用于执行编辑和删除操作。下面显示的方法首次在数据网格视图中添加记录。

private void AddFirstRecord()
    {
        dtEmployee = new DataTable();
        dtEmployee.Columns.Add("First Name");
        dtEmployee.Columns.Add("Last Name");
        dtEmployee.Columns.Add("City");
        dtEmployee.Columns.Add("Date of birth");
        dtEmployee.Columns.Add("Email");
        dtEmployee.Columns.Add("Gender");
        dtEmployee.Columns.Add("Contact");
        dtEmployee.Columns.Add("Income");
        dtEmployee.Columns.Add("Experience");
        DataRow drEmployee = dtEmployee.NewRow();
        drEmployee[0] = txtFirstName.Text;
        drEmployee[1] = txtLastName.Text;
        drEmployee[2] = txtCity.Text;
        drEmployee[3] = mtxtDateofBirth.Text;
        drEmployee[4] = txtEmail.Text;
        if (rdbMale.Checked)
            drEmployee[5] = rdbMale.Text;
        else
            drEmployee[5] = rdbFemale.Text;
        drEmployee[6] = mtxtContact.Text;
        drEmployee[7] = txtIncome.Text;
        drEmployee[8] = cmbExperience.Text;
        dtEmployee.Rows.Add(drEmployee);
        dataGridView1.DataSource = dtEmployee;
        dataGridView1.AutoGenerateColumns = false;
        DataGridViewLinkColumn EditLink = new DataGridViewLinkColumn();
        EditLink.UseColumnTextForLinkValue = true;
        EditLink.HeaderText = "Edit";
        EditLink.LinkBehavior = LinkBehavior.SystemDefault;
        EditLink.Text = "edit";
        dataGridView1.Columns.Add(EditLink);
        DataGridViewLinkColumn DeleteLink = new DataGridViewLinkColumn();
        DeleteLink.UseColumnTextForLinkValue = true;
        DeleteLink.HeaderText = "Delete";
        DeleteLink.LinkBehavior = LinkBehavior.SystemDefault;
        DeleteLink.Text = "delete";
        dataGridView1.Columns.Add(DeleteLink);
        dataGridView1.DataSource = dtEmployee;
    }//AddFirstRecord       

AddNewRecord() 方法用于在添加第一条记录后添加记录。

  private void AddNewRecord()
    {
        DataRow drEmployee = dtEmployee.NewRow();
        drEmployee[0] = txtFirstName.Text;
        drEmployee[1] = txtLastName.Text;
        drEmployee[2] = txtCity.Text;
        drEmployee[3] = mtxtDateofBirth.Text;
        drEmployee[4] = txtEmail.Text;
        if (rdbFemale.Checked)
            drEmployee[5] = rdbFemale.Text;
        else
            drEmployee[5] = rdbMale.Text;
        drEmployee[6] = mtxtContact.Text;
        drEmployee[7] = txtIncome.Text;
        drEmployee[8] = cmbExperience.Text;
        dtEmployee.Rows.Add(drEmployee);
        dataGridView1.DataSource = dtEmployee;
    }

我已经编写了下面显示的用于触发编辑和删除链接单元格的方法,但它在 if (dtEmployee.Columns[columnIndex] 行显示错误 IndexOutOfRangeException。ColumnName == "Edit") 表示它找不到第 9 列,并且在其他行如果 (dtEmployee.Columns[e.ColumnIndex]。列名 == "删除"),表示找不到第 10 列。

 private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        try
        {
            int columnIndex = e.ColumnIndex;
            int rowIndex = dataGridView1.CurrentCell.RowIndex;
            if (dtEmployee.Columns[columnIndex].ColumnName == "Edit")
            {
                dataGridView1.BeginEdit(true);
                dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
            }
            else if (dtEmployee.Columns[e.ColumnIndex].ColumnName == "Delete")
            {
                if (MessageBox.Show("Are you sure you want to delete?", "Deleting...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    dtEmployee.Rows.RemoveAt(rowIndex);
            }
        }
        catch(Exception ex)
        {
            MessageBox.Show("Sorry for the inconvenience" + ex.ToString() );
        }
    }

System.IndexOutOfRangeException 错误在 winform 中单击 DataGridView

try
    {
        int columnIndex = e.ColumnIndex;
        int rowIndex = dataGridView1.CurrentCell.RowIndex;
        if (dataGridView1.Columns[columnIndex].HeaderText == "Edit")
        {
            dataGridView1.BeginEdit(true);
            dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
        }
        else if (dataGridView1.Columns[e.ColumnIndex].HeaderText == "Delete")
        {
            if (MessageBox.Show("Are you sure you want to delete?", "Deleting...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                dtEmployee.Rows.RemoveAt(rowIndex);
        }
    }
    catch(Exception ex)
    {
        MessageBox.Show("Sorry for the inconvenience" + ex.ToString() );
    }

尝试将 CellContentClick Event Handler 替换为此函数。

  private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
        try
        {
            int columnIndex = e.ColumnIndex;
            int rowIndex = e.RowIndex;
            string columnValue = dtEmployee.Rows[rowIndex].Cells[columnIndex].HeaderText.ToString();
            if (columnValue == "Edit")
            {
                dataGridView1.BeginEdit(true);
                dataGridView1.EditingControlShowing += new DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
            }
            else if (columnValue == "Delete")
            {
                if (MessageBox.Show("Are you sure you want to delete?", "Deleting...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    dtEmployee.Rows.RemoveAt(rowIndex);
            }
        }
        catch(Exception ex)
        {
            MessageBox.Show("Sorry for the inconvenience" + ex.ToString() );
        }
    }