473,569 Members | 2,901 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with Delete in DataTable and DataGrid

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()

sqlRetrieveDist ributionListMem bers.Parameters ("@distID").Val ue = distID

sqlRetrieveDist ributionListMem bers.Parameters ("@distListID") .Value =
distListID

daDistList.Sele ctCommand = sqlRetrieveDist ributionListMem bers

daDistList.Fill (dsDistList, "Members")

dvMembers = New DataView(dsDist List.Tables("Me mbers"), "", "",
DataViewRowStat e.CurrentRows)

Datagrid2.DataS ource = dvMembers

Datagrid2.DataK eyField = "distID"

Datagrid2.DataB ind()

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.DataK eys(0)

End If

Next

Any help would be really appreciated.

TIA

Nov 17 '05 #1
1 1801
Use

For Each dgi In Datagrid2.Items

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

If cb.Checked = True Then

Respones.Write( DataGrid2.DataK eys(dgi.ItemInd ex) '
Response.Write( Datagrid2.DataK eys(0) See dgiItemIndex instead of 0 you used.

End If
HTH
Prasad

"J" <nospam> wrote in message news:#D******** ******@TK2MSFTN GP09.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()

sqlRetrieveDist ributionListMem bers.Parameters ("@distID").Val ue = distID

sqlRetrieveDist ributionListMem bers.Parameters ("@distListID") .Value =
distListID

daDistList.Sele ctCommand = sqlRetrieveDist ributionListMem bers

daDistList.Fill (dsDistList, "Members")

dvMembers = New DataView(dsDist List.Tables("Me mbers"), "", "",
DataViewRowStat e.CurrentRows)

Datagrid2.DataS ource = dvMembers

Datagrid2.DataK eyField = "distID"

Datagrid2.DataB ind()

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.DataK eys(0)

End If

Next

Any help would be really appreciated.

TIA

Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5124
by: Anita C | last post by:
Hi, How do I associate or map a specific column in a datatable to a particular element present in an xml document - to read into a datatable as well as write from the datatable to the xml element? Also, how can I associate all the attributes and their values of a particular element to the Name & Value columns of a datatable - to read into a...
0
1322
by: Mark Jones | last post by:
I'm trying to delete a row from a DataGrid using a ButtonColumn with CommandName="Delete" by deleting the row in the underlying DataTable datasource, but it doesn't seem to be working at the minute. I'm using session data to fill the DataTable which subsequently fills the DataGrid. The code is as follows (with "dgBasket" as the DataGrid): ...
2
2559
by: Mark Jones | last post by:
I'm trying to delete data from a DataGrid using a ButtonColumn with a CommandName="Delete" but it's not working a the min. I'm using session data which fills a DataTable which then fills the DataGrid. The code is as follows (dgBasket is the DataGrid): private void Page_Load(object sender, System.EventArgs e) { DataTable myTable =...
6
4631
by: JenHu | last post by:
Hi experts, I want to add a delete button in my datagrid, and before it deletes from database, I want to have a confirm dialog box for this deletion. I am entry level to the vb.net, and don't know java or c#. The problem is, when I tested the program, there is no pop-up confirmation box for the deletion, can you show me what' wrong with...
6
1366
by: Ryan Smith | last post by:
I am having a problem with a datagrid I am trying to use. I have an Access DB with two records I want to display. All that is displayed is the heading for the datagrid. I can't seem to display the actual data. Below is the Code Behind Portion: Imports System.Data.OleDb Public Class Units Inherits System.Web.UI.Page
0
2255
by: Suzanne | last post by:
I'd like to know how can I put up a confirmation question when the user tries to delete a row in the datagrid by clicking on the row header and pressing the Delete key? I have found this code on a windows forms FAQ site : Public Class DataGrid_Custom Inherits DataGrid Private Const WM_KEYDOWN = &H100
2
1662
by: Terry Olsen | last post by:
Most of the Apps I write are 'Data Mining' apps. In VB6, I used the MSFlexGrid a lot. Now i'm trying to understand the use of the DataGrid, DataTable, & DataSet. Using examples I've gotten from here and from other web sites, I've been able to populate a DataGrid with a CSV file (though I can't say I fully understand the code used to do...
5
2713
by: manmit.walia | last post by:
Hello All, I am stuck on a conversion problem. I am trying to convert my application which is written in VB.NET to C# because the project I am working on currently is being written in C#. I tried my best to convert it, but somehow the app does not work. I am also not getting any errors when I complie thus, letting me know I am on the write...
5
3100
by: rn5a | last post by:
The .NET 2.0 documentation states the following: When using a DataSet or DataTable in conjunction with a DataAdapter & a relational data source, use the Delete method of the DataRow to remove the row. The Delete method marks the row as Deleted in the DataSet or DataTable but does not remove it. Instead when the DataAdapter encounters a row...
0
7693
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7917
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7665
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6277
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5501
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.