illumi
illumi

Reputation: 458

Getting the row number of the dataset by using datagridview's currentrow property

please help me with this problem. I have a datagridview that is bounded to a dataset and when i select an item to the datagridview to delete, I get the correct item. But when i sorted the datagridview to some field, it return a different item. Here is my code:

DSet.Tables("sec_company_address").Rows(sec_company_address.CurrentRow.Index).Delete()

Edit: i try this line but it brings up an error.

Dim xID As String = sec_role_module.CurrentRow.Cells("nID").Value.ToString
sDataSet.Tables(sec_role_module.Name).Rows.Find(xID).Delete()

enter image description here

Upvotes: 0

Views: 3706

Answers (2)

John Woo
John Woo

Reputation: 263843

Before anything else, you must set Primary column in the sec_company_address so that DELETE will work. and try this:

' change columnName to your column which is the Unique Identifier
dim xID as string = dataGridView1.CurrentRow.Cells("columnName").Value.Tostring 
DSet.Tables("sec_company_address").Rows.Find(xID).Delete()
DSet.Tables("sec_company_address").AcceptChanges()

UPDATE

how about this?

    For Each xRow As DataRow In sec_role_module.SelectedRows
        sDataset.Tables(sec_role_module.Name).Rows.Remove(xRow)
        sDataset.AcceptChanges()
    Next

Upvotes: 1

Maarten Kesselaers
Maarten Kesselaers

Reputation: 1251

By changing the sorting of your datagridview, you refer to another item in your dataset, because your dataset is still the same as the initial load.

How can you fix this? You could use a unique identifier in your first column, which you can refer to for the deletion of the row in the dataset.

Upvotes: 0

Related Questions