By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,282 Members | 1,548 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,282 IT Pros & Developers. It's quick & easy.

Help with Delete in DataTable and DataGrid

P: n/a
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("@di stID").Value = distID

sqlRetrieveDistributionListMembers.Parameters("@di stListID").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

Nov 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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

"J" <nospam> wrote in message news:#D**************@TK2MSFTNGP09.phx.gbl...
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("@di stID").Value = distID

sqlRetrieveDistributionListMembers.Parameters("@di stListID").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

Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.