Delete Items in a DataGrid Web Server Control

T

tomcarr1

In this walkthrough on "Allowing Users to Delete Items in a DataGrid
Web Server Control" at:

ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/vbcon/html/vbtskallowinguserstodeleteitemsindatagridwebcontrol.htm

it leaves out some very important code. Here is what it gives you:

' Visual Basic
Private Sub DataGrid1_DeleteCommand(ByVal source As Object, _
ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) _
Handles DataGrid1.DeleteCommand
Dim index As Integer
index = e.Item.ItemIndex
' Add code to delete data from data source.
DataGrid1.DataBind()
End Sub

Where is says "Add code to delete data from data source" I have no idea
what to add. Can someone tell me what goes there, or point me to a
walkthough or example that goes into more detail?
 
N

Norman Yuan

It all depends on what type of data is bound to the DataGrid, and how yo can
identify the actual data/record according to the ItemIndex of the DataGrid.

A simple case would be a DataTable is bound to this DataGrid and the
DataRow's ID column is bound to the first column of the DataGrid (and set
its Visible=False).

So, you could do this:

Dim id As String=e.Item.Cells(0).Text
Dim dr As DataRow=theDataTable.Select("IDColumn=" & id)(0)
dr.Delete

You would decide if oe when to update the deletion back to the database.
 
T

tomcarr1

Thanks Norman. I got it to delete using this code:

Dim index As Integer
index = e.Item.ItemIndex
' Add code to delete data from data source.
DsTasks1.Tasks.Rows(index).Delete()
Me.OleDbDataAdapter1.Update(DsTasks1)
' Refreshes the grid
DataGrid1.DataBind()
 

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

Top