Search code examples
c#winformsdata-binding.net-3.5system.data.datatable

Specifying the records to be deleted from a database using the TableAdapter.Update method


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

Solution

  • 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.