473,378 Members | 1,384 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Deleting a row in a datagrid

Hi,
I have a datagrid populated from a dataset. On every row in the grid I have
a delete button. When the user presses on the delete button I remove the
row from the dataset and rebind the datagrid.
The problem is that after a couple of delete the index in the dataset does
not match the index in the grid and the wrong record i deleted from the
dataset. How can I solve this Problem? I am using the following procedure:

Private Sub dg1_ItemCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles dg1.ItemCommand
If e.CommandName.ToString = "Delete" Then
Dim dset As DataSet
dset = CType(Session("VideoDset"), DataSet)
dset.Tables(0).Rows(e.Item.ItemIndex()).Delete()

dg1.DataSource = dset
Session("videoDset") = dset
dg1.DataBind()
End If
End Sub

Thanks
NDady

--
Message posted via http://www.dotnetmonster.com
Nov 19 '05 #1
2 1675
NDady:
I'm not 100% sure, but I think it's because your Rows aren't actually
deleted, simply marked for deletion. Let me explain, let's say you have 4
rows, at first it'll look like this:

Grid-Row-0 == DataSet-Row-0
Grid-Row-1 == DataSet-Row-1
Grid-Row-2 == DataSet-Row-2
Grid-Row-3 == DataSet-Row-3

So when you get e.Item.ItemIndex the first time, it'll delete the correct
record in the dataset. However, when you rebind, thinks fall out of synch.
Let's say Row 1 was deleted, it'll now look like:

Grid-Row-0 == DataSet-Row-0
Grid-Row-1 == DataSet-Row-2
Grid-Row-2 == DataSet-Row-3

You see, DataSet-Row-1 still exist, but it isn't bound/displayed. So when
you delete Grid-Row-1, you are getting index "1", and deleting data row "1".

What I would do is place a CommandArgument in the bottom which is the unique
Id of the row and access the value from e.CommandArgument.

Karl
"NDady via DotNetMonster.com" <fo***@nospam.DotNetMonster.com> wrote in
message news:31******************************@DotNetMonste r.com...
Hi,
I have a datagrid populated from a dataset. On every row in the grid I
have
a delete button. When the user presses on the delete button I remove the
row from the dataset and rebind the datagrid.
The problem is that after a couple of delete the index in the dataset does
not match the index in the grid and the wrong record i deleted from the
dataset. How can I solve this Problem? I am using the following procedure:

Private Sub dg1_ItemCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles
dg1.ItemCommand
If e.CommandName.ToString = "Delete" Then
Dim dset As DataSet
dset = CType(Session("VideoDset"), DataSet)
dset.Tables(0).Rows(e.Item.ItemIndex()).Delete()

dg1.DataSource = dset
Session("videoDset") = dset
dg1.DataBind()
End If
End Sub

Thanks
NDady

--
Message posted via http://www.dotnetmonster.com

Nov 19 '05 #2
Hi Karl,
How can I implement your solution in my code. You see I am quite new in
..Net.
Is there any way to re-index the dataset so it will look like the datagrid?

Regards
NDady

--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...p-net/200506/1
Nov 19 '05 #3

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

Similar topics

3
by: Stephen | last post by:
I've got a datagrid with a remove button and I would like to add some code in the code behind page so as whenthe button is clicked the corresponding row in the datagrid is removed. The Datagrid is...
1
by: Junkguy | last post by:
I'm having difficulty deleting rows from a datagrid. I want to put a "delete" button on a form and achieve the same functionality as hitting the "delete" key on the keyboard for the selected row of...
5
by: Mojtaba Faridzad | last post by:
Hi, with SetDataBinding( ) a DataGrid shows a DataView. user can select some rows in the grid by holding cotrol key. when user clicks on Delete button, I should delete all selected rows. I am...
2
by: Alex K. | last post by:
I am using datagrid bound to a datatable: DataTable dt = ...; MyGrid.DataSource = new DataView(dt); Now I am trying to intercept delete operation, when user highlights a row and presses...
0
by: Hrvoje Vrbanc | last post by:
Hello, this is a problem I came upon while building a site based on MCMS 2002 but it's not strictly MCMS-oriented: I have a page that displays a certain content in presentation mode but when an...
0
by: Robert Batt | last post by:
Hello I have a problem deleting rows from a datagrid that is bound to a dataset. The datagrid updates and inserts just fin so the binding to the dataset must be ok. However when I try to delete a...
1
by: Robert Batt | last post by:
Hello, I have a problem deleting from a datagrid bound to a dataset. then datagrid is bound as follows MyDatagrid.DataSource = Mydataset.Tables(strtable this works fine for adding and...
10
by: Nick | last post by:
Hello, Please pardon my ignorance as I'm sure this is easy to do. I have a datagrid where I want to let the user delete columns. I added a context menu to the datagrid that has a delete option....
9
by: Hamed | last post by:
Hello I have a DataGrid that a is bound to a DataTable. Some of the rows in the DataTable should not be deleted. How can I prohibit deleting of some identified rows? The problem could be...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.