Help with Delete in DataTable and DataGrid

J

J

Hi,

Can someone please help with a problem I have. The BOL I think are useless
in explaining what I want to do.

I have a datagrid, that is bound to a dataview. I have a checkbox as a
template column and a button marked 'delete'.

What I want to do, is be able to select a series of rows from the datagrid,
using the checkboxes, and delete them from the table when I click the delete
button.

I 'think' I'm right in my understanding that I have to create a primary key
on the datatable, and search using this key, then mark the row that is found
for deletion.

Trouble is, the BOL point to examples of if I was to use the default
'Select' command that is bound to the datagrid, and not using template
controls. So I can't see what datakey(s) are selected when I select them
using the checkboxes.

At present, my code is...

(To bind the data to the grid)

SqlConnection1.Open()

sqlRetrieveDistributionListMembers.Parameters("@distID").Value = distID

sqlRetrieveDistributionListMembers.Parameters("@distListID").Value =
distListID

daDistList.SelectCommand = sqlRetrieveDistributionListMembers

daDistList.Fill(dsDistList, "Members")

dvMembers = New DataView(dsDistList.Tables("Members"), "", "",
DataViewRowState.CurrentRows)

Datagrid2.DataSource = dvMembers

Datagrid2.DataKeyField = "distID"

Datagrid2.DataBind()

SqlConnection1.Close()



To delete the data from the grid....



Dim dgi As DataGridItem

Dim cb As CheckBox

Dim i As Integer = 0

Dim rowID As Integer

Dim dr As DataRow

For Each dgi In Datagrid2.Items

cb = CType(dgi.Cells(0).Controls(1), CheckBox)

If cb.Checked = True Then

Response.Write(Datagrid2.DataKeys(0)

End If

Next



Any help would be really appreciated.



TIA
 
P

Prasad

Use

For Each dgi In Datagrid2.Items

cb = CType(dgi.Cells(0).Controls(1), CheckBox)

If cb.Checked = True Then

Respones.Write(DataGrid2.DataKeys(dgi.ItemIndex) '
Response.Write(Datagrid2.DataKeys(0) See dgiItemIndex instead of 0 you used.

End If


HTH
Prasad
 

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

Staff online

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top