Windows Develop Bookmark and Share   
 index > Windows Forms General > How to Delete Records from Sorted DataGrid, , Programmatically
 

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?
Eric Cann  Saturday, May 27, 2006 6:53 AM

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

Mamta M  Monday, May 29, 2006 6:52 AM

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

Stefano Padovan  Saturday, May 27, 2006 12:59 PM

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!

Eric Cann  Monday, May 29, 2006 6:19 AM

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

Mamta M  Monday, May 29, 2006 6:52 AM
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?
Eric Cann  Monday, May 29, 2006 7:47 AM

You can use google to search for other answers

Custom Search

More Threads

• Problem with UserControl and Panel, C#
• Multithread output display
• How do you use the PropertyDescriptorCollection?
• Using the Updater Application Block
• Open Child form
• Get text from datagrid cell
• Can't set value of RichTextBox.Lines[index]
• Combining two bitmaps
• Custom Control Events
• how to avoid linking to code files, between two projects, of different types, doing the same thing?