DataKeys in Datagrid

A

Atif Jalal

I have a datagrid with a checkbox as one of the column. When the
checkboxes are selected and a delete button at the bottom of the grid
is clicked, I want the selected row in grid to be deleted. I am having
trouble in determining the key of the selected record. I get the error
index out of range. The code is as follows in VB.net. Your help
appreciated.

For Each dgi In MySearchesGrid.Items
cb = CType(dgi.Cells(0).Controls(1), CheckBox)
If cb.Checked Then
'' Determine the key of the selected record ...
bookid = CType(MySearchesGrid.DataKeys(i), Integer)
<----- get an error at this point
' ... get a pointer to the corresponding dataset
record ...
dr = Me.dsMySearches.Tables(0).Rows(bookid)
' ... and delete it.
dr.Delete()
End If
i += 1
Next
 
S

Saravana

Instead of using datakeyfield to get the primary key value, you are
accessing datakey.

So Instead of this statement
bookid = CType(MySearchesGrid.DataKeys(i), Integer)

Use this statement in your code, this should work.

bookid = CType(MySearchesGrid.DataKeyField(i),Integer)
 
P

Prasad

Hi

Instead of use MySearchesGrid.DataKeys(i) try using
MySearchesGrid.DataKeys(dgi.ItemIndex)

Prasad
HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top