Specifying the records to be deleted from a database using the TableAdapter.Update method
-
06-10-2020 - |
Frage
I have been messing with this for quite some time now and it's getting less and less fun; I followed the MSDN guide for deleting a row from a datagrid. And it works for any row however I am not able to specify the row... essentially I can delete random rows by using the CurrentIndex
parameter anything I try to be more specific gets me a conversion errors.
In a nut shell 'FindByID' (my Primary Key) gives me 'object to long' errors etc. Can't nail down the row I want removed.
//int ThisRow = radGridView1.CurrentIndex.Value;
// Locate row for deletion
VSConnectorDataSet.TestTableRow oldTestTableRow;
oldTestTableRow = vSConnectorDataSet.TestTable.FindByID(
Int64.Parse(radGridView1.CurrentRow.Cells["ID"].Value));
// Delete the row from the dataset
oldTestTableRow.Delete();
// Delete from database
this.testTableTableAdapter1.Update(this.vSConnectorDataSet.TestTable);
//DataRow rowDel=vSConnectorDataSet.TestTable.Rows[ThisRow];
//rowDel.Delete();
//testTableTableAdapter1.Update(vSConnectorDataSet);
Lösung
Int64.Parse
takes a string only. Perhaps try:
long selRowVal = (long)radGridView1.CurrentRow.Cells["ID"].Value;
oldTestTableRow = vSConnectorDataSet.TestTable.FindByID(selRowVal);
or
Int64.Parse(radGridView1.CurrentRow.Cells["ID"].Value.ToString());
It's not clear what datatype the Cell.Value
is. Perhaps an explicit cast might help.
Andere Tipps
having found how to Convert.ToInt64 I am now able to target my deleted row via the bound dataset-tableadapter-database method.
private void DeleteToolStripButton_Click(object sender, EventArgs e)
{
long ThisRow = Convert.ToInt64((radGridView1.CurrentRow.Cells["ID"].Value));
DialogResult DelEntry = MessageBox.Show("Do you want to delete the entry titled '" + radGridView1.CurrentRow.Cells["SparesTitle"].Value + "'?", "Delete this entry?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
switch (DelEntry)
{
case DialogResult.OK:
// Locate row for deletion
VSConnectorDataSet.TestTableRow oldTestTableRow;
oldTestTableRow = vSConnectorDataSet.TestTable.FindByID(ThisRow);
// Delete the row from the dataset
oldTestTableRow.Delete();
// Delete from database
this.testTableTableAdapter1.Update(this.vSConnectorDataSet.TestTable);
break;
//
// To Do - Give Slected to another row; having just deleted our 'CurrentRow'
//
case DialogResult.Cancel:
break;
}
}