0
votes

I have a DataGridView named dataGridView1 where I have made 9 columns for getting the details of employees and 2 additional columns for EditLinkCellColumn and DeleteLinkCellColumn for performing edit and delete operations. The method shown below adds the record for the first time in the datagridview.

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       

The method AddNewRecord() is used for adding records after the first record has been added.

  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;
    }

I have written the method shown below for firing the edit and delete link cells but it shows an error IndexOutOfRangeException at the line if (dtEmployee.Columns[columnIndex].ColumnName == "Edit") saying that it cannot find column 9 and also at line else if (dtEmployee.Columns[e.ColumnIndex].ColumnName == "Delete") saying it cannot find column 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() );
        }
    }
2

2 Answers

0
votes
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() );
    }
0
votes

Try replacing your CellContentClick Event Handler with this.

  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() );
        }
    }