C# Best way to delete a row from typed dataset bounded by binding source

10,683

Solution 1

You could also delete directly using the BindingSource :

bsAddressBook.RemoveCurrent();

Solution 2

I think you can shorten this using the Row property of the DataRowView.

// Get the value of the PK from the currently selected row
DataRowView drv = (DataRowView)bsAddressBook.Current;

DataRow drDelete = drv.Row;
dsCATDialer.AddressBook.Rows.Remove(drDelete);
Share:
10,683
ant2009
Author by

ant2009

Updated on June 05, 2022

Comments

  • ant2009
    ant2009 almost 2 years

    C# 2008 SP1.

    I am deleting a row from a row that is currently selected on a datagridview.

    I am using a Typed dataset and my datagridview is bounded to a binding source.

    However, I think my technique is not the best, even though it works.

    Many thanks for any advice,

     DataRow[] drDelete;
                // Get the value of the PK from the currently selected row
                DataRowView drv = (DataRowView)bsAddressBook.Current;
                int index = Convert.ToInt16(drv[0]);
    
                // Find the actual data row from the primary key and delete the row
                drDelete = dsCATDialer.AddressBook.Select(string.Format("ID = '{0}'", index));
                dsCATDialer.AddressBook.Rows.Remove(drDelete[0]);