views:

38

answers:

2

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

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.

p.campbell
@p.campbell you are definatley on the correct track.
Confussedinwales
@confussedinwales: great, glad to have helped! On StackOverflow, you upvote and 'mark as answer' those people who've helped you find your solution. In this case, it looks like casting/converting to a `long` was correct. I'd be happy to take an upvote and green checkmark if you're willing!
p.campbell
@p.cambell I did try to VoteUp after reading your response however it says it requires 15 rep!?! So now I realised you can have more than one answer..... :)
Confussedinwales
A: 

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