0

I have 2 DataGridView namely dgvEmpEnrollees and dgvEmpMustAttend which have a DataTable as data sources namely dtEnrollees and dtMustAttend respectively.

I have a btnRemove which removes the currently selected row in the dgvEmpEnrollees datagridview dtEnrollees DataTable and immediately updates the Datagridview and I want this same row to be added in the second DT and DGV.

Note: The 2 DataTables are cloned:

btnRemove code snippet is as follows:

    private void btnRemove_Click(object sender, EventArgs e)
        {
            DataRowView currentDataRowView = (DataRowView)dgvEmpEnrollees.CurrentRow.DataBoundItem;
            DataRow row = currentDataRowView.Row;

            dtEnrollees.Rows.Remove(row);
            dgvEmpEnrollees.DataSource = null;
            dgvEmpEnrollees.DataSource = dtEnrollees;


            dtMustAttend.Rows.InsertAt(row,0);
            dgvEmpMustAttend.DataSource = null;
            dgvEmpMustAttend.Rows.Clear();
            dgvEmpMustAttend.DataSource = dtMustAttend;

        }

This row belongs to another table error in C# prompts afterwards.

I have also tried the dtMustAttend.ImportRow(row); method, it doesn't prompt an error but the row won't add. Any help would be much appreciated.

Sai
  • 103
  • 3
  • 14

2 Answers2

1

The row is really deleted from the DataTable with this line:

dtEnrollees.Rows.Remove(row);

Also any data is removed from the row. If you check the ItemArray-property after Remove(), it will state:

'test.ItemArray' threw an exception of type 'System.Data.RowNotInTableException'

Another problem is that the row still has the Table-property set to the table where it was. At first you used the InsertAt()-method, but it fails because the Table-property is still set to other table.

When you used the ImportRow(), the row had already cleared it's ItemArray, so you were trying to add an empty row.

W0lfw00ds
  • 2,018
  • 14
  • 23
-1

I found the solution but I don't know what seems to be the explanation behind it.

I tried interchanging the import and remove codes (import comes first) and it worked.

The working code snippet:

 private void btnRemove_Click(object sender, EventArgs e)
        {
            DataRowView currentDataRowView = (DataRowView)dgvEmpEnrollees.CurrentRow.DataBoundItem;
            DataRow row = currentDataRowView.Row;

            dtMustAttend.ImportRow(row);
            dgvEmpMustAttend.DataSource = null;
            dgvEmpMustAttend.Rows.Clear();
            dgvEmpMustAttend.DataSource = dtMustAttend;

            dtEnrollees.Rows.Remove(row);
            dgvEmpEnrollees.DataSource = null;
            dgvEmpEnrollees.DataSource = dtEnrollees;

        }

I don't know why the code doesn't work if the remove transaction comes first:

 private void btnRemove_Click(object sender, EventArgs e)
        {
            DataRowView currentDataRowView = (DataRowView)dgvEmpEnrollees.CurrentRow.DataBoundItem;
            DataRow row = currentDataRowView.Row;

            dtEnrollees.Rows.Remove(row);
            dgvEmpEnrollees.DataSource = null;
            dgvEmpEnrollees.DataSource = dtEnrollees;

            dtMustAttend.ImportRow(row);
            dgvEmpMustAttend.DataSource = null;
            dgvEmpMustAttend.Rows.Clear();
            dgvEmpMustAttend.DataSource = dtMustAttend;

        }

Any explanation would be much appreciated.

Sai
  • 103
  • 3
  • 14