Deleting a Record From a Data Table

C

Ctal

I have an app that populates several data tables on load. Each of
these are bound to a datagrid. Above each datagrid I have several
text boxes that display the data for the active row. There are
buttons to allow the user to add, update, and delete a row in the
grid. I have an event on cell changed associated with the grid that
updates the text boxes so they are always current.

When the delete button is used I'm getting an error that ""Deleted row
information cannot be accessed through the row."

Here's what I want to happen:

1. User selects a row
2. The contents of the row appear in the appropiate text boxes
3. User presses delete button
4. Row is marked for deletion when dataset changes are written back
and not shown in the grid
5. text boxes are updated to show the next row.

Here's my method to update the text boxes:

UpdateTextBoxes()
{
int curSelection = dgMembForm.CurrentRowIndex;
DataRow r = dTable.Rows[curSelection];

txtMemberName.Text = r["Account"].ToString();
rtbMemberFormula.Text = r["Member Formula"].ToString();
)

The event on cell changed just calls this method

Heres the code for the delete button:

private void btnDelete_Click(object sender, EventArgs e)
{
DataRow selectedRow = this.SelectedRow(dTable,
dgMembForm);
int sRow = dgMembForm.CurrentCell.RowNumber;
dTable.Rows[sRow].Delete();
dgMembForm.CurrentRowIndex = 0;
UpdateTextBoxes();
}


I added the piece to set index to 0 to solve a similar problem when
the last row was deleted.
 
V

VJ

So what happens if they select the first row and delete?.. I don't think
what your doing will work all situations.. You may want to select next
available row or something. Also after delete call AcceptChanges()

VJ
 
C

Ctal

Ctal said:
I have an app that populates several data tables on load. Each of
these are bound to a datagrid. Above each datagrid I have several
text boxes that display the data for the active row. There are
buttons to allow the user to add, update, and delete a row in the
grid. I have an event on cell changed associated with the grid that
updates the text boxes so they are always current.
When the delete button is used I'm getting an error that ""Deleted row
information cannot be accessed through the row."
Here's what I want to happen:
1. User selects a row
2. The contents of the row appear in the appropiate text boxes
3. User presses delete button
4. Row is marked for deletion when dataset changes are written back
and not shown in the grid
5. text boxes are updated to show the next row.
Here's my method to update the text boxes:
UpdateTextBoxes()
{
int curSelection = dgMembForm.CurrentRowIndex;
DataRow r = dTable.Rows[curSelection];
txtMemberName.Text = r["Account"].ToString();
rtbMemberFormula.Text = r["Member Formula"].ToString();
)
The event on cell changed just calls this method
Heres the code for the delete button:
private void btnDelete_Click(object sender, EventArgs e)
{
DataRow selectedRow = this.SelectedRow(dTable,
dgMembForm);
int sRow = dgMembForm.CurrentCell.RowNumber;
dTable.Rows[sRow].Delete();
dgMembForm.CurrentRowIndex = 0;
UpdateTextBoxes();
}
I added the piece to set index to 0 to solve a similar problem when
the last row was deleted.- Hide quoted text -

- Show quoted text -

So what happens if they select the first row and delete?.. I don't think
what your doing will work all situations.. You may want to select next
available row or something. Also after delete call AcceptChanges()

VJ

I should have added that the problem occurs only when they delete the
first record. When UpdateTextBoxes() runs after that, it gives the
message about Deleted row access.

I save changes back to the database through another method, hoping to
give they user the opportunity to make multiple changes before I
update the data adapter.

Assuming the first row in the data table is marked for delete how do I
get the next row?
 
V

VJ

the first row is 0.. So you will have to select next row which is 1 before
AcceptChanges() and 0 after AcceptChanges().

VJ

Ctal said:
I have an app that populates several data tables on load. Each of
these are bound to a datagrid. Above each datagrid I have several
text boxes that display the data for the active row. There are
buttons to allow the user to add, update, and delete a row in the
grid. I have an event on cell changed associated with the grid that
updates the text boxes so they are always current.
When the delete button is used I'm getting an error that ""Deleted row
information cannot be accessed through the row."
Here's what I want to happen:
1. User selects a row
2. The contents of the row appear in the appropiate text boxes
3. User presses delete button
4. Row is marked for deletion when dataset changes are written back
and not shown in the grid
5. text boxes are updated to show the next row.
Here's my method to update the text boxes:
UpdateTextBoxes()
{
int curSelection = dgMembForm.CurrentRowIndex;
DataRow r = dTable.Rows[curSelection];
txtMemberName.Text = r["Account"].ToString();
rtbMemberFormula.Text = r["Member Formula"].ToString();
)
The event on cell changed just calls this method
Heres the code for the delete button:
private void btnDelete_Click(object sender, EventArgs e)
{
DataRow selectedRow = this.SelectedRow(dTable,
dgMembForm);
int sRow = dgMembForm.CurrentCell.RowNumber;
dTable.Rows[sRow].Delete();
dgMembForm.CurrentRowIndex = 0;
UpdateTextBoxes();
}
I added the piece to set index to 0 to solve a similar problem when
the last row was deleted.- Hide quoted text -

- Show quoted text -

So what happens if they select the first row and delete?.. I don't think
what your doing will work all situations.. You may want to select next
available row or something. Also after delete call AcceptChanges()

VJ

I should have added that the problem occurs only when they delete the
first record. When UpdateTextBoxes() runs after that, it gives the
message about Deleted row access.

I save changes back to the database through another method, hoping to
give they user the opportunity to make multiple changes before I
update the data adapter.

Assuming the first row in the data table is marked for delete how do I
get the next row?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top