How to Delete Records from Sorted DataGrid, , Programmatically

I have a Delete button on a form and a datagrid (forms). When I sort the datagrid and run my Delete Routine, I end up deleting with reference from the row number. So if A (which it's absolute positions in 2) is sorted and is now in row 10, when I delete it, it removes which ever was in row 10. How would I get the absolute row position of a row in a sorted datagrid

Answer this question

How to Delete Records from Sorted DataGrid, , Programmatically

  • Magne_01

    I tried what you wrote, but it does the same thing (deletes the wrong row). Lets say I have a Table with a text field as column 1 and had entries (in order) d, e, b, c, a. When I sort the datagrid by clicking on the caption, it sorts the entries (a, b, c, d, e). When I run the code index = Me.BindingContext.Item(myDS.myTABLE).Current.Position, index = 0. If I delete row 0, it deletes d.

    Help!


  • hupo1982

    Hello,

    you can get the actual row's index from something like this (where myDS.myTABLE is your datatable bound to the datagrid and Me refers to your windows form)

    Dim index As Integer = 0

    index = Me.BindingContext.Item(myDS.myTABLE).Current.Position

    HTH

    Stefano



  • Santos

    Ensure that the data in the underlying DataTable is also sorted. You can make use of the Select method:

    datatable.Select(filterExp, sortExp, rowstatevalue)

    Refer to the following links

    http://msdn.microsoft.com/library/default.asp url=/library/en-us/vbcon/html/vbtskfilteringsortingdirectlyindatatable.asp

    http://msdn2.microsoft.com/en-us/library/69c06922.aspx

    Hope that helped.

    Regards,

    Mamta


  • Chris7432

    It seems like the ticket but what datagrid event should I use to detect which column has been sorted, and whether it's sorted ascending/descending
  • How to Delete Records from Sorted DataGrid, , Programmatically