?
Solved

delete datarow from datatable in datagrid

Posted on 2007-08-01
7
Medium Priority
?
1,865 Views
Last Modified: 2013-11-26
I have a datagrid: dgprogress, that is binded to a datatable and the datagrid has a delete button which i would like to have a datarow deleted when the delete button is click by the corresponding delete button.

Sub deleteclicked(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
        'when a user clicks the delete next to an entry it is deleted from the datatable and the database
        Dim temprow As DataRow
        Dim tempds As DataSet

        tempds = Session("dataset_ds")

        Dim iid As String = dgprogress.Items(e.Item.ItemIndex).Cells(1).Text 'Grab the id from the hidden column
        ' Dim remove As Integer = Convert.ToInt32(dgprogress.Items(e.Item.ItemIndex))
        ' lblerrormessage.Text = e.Item.ItemIndex
        lblerror.Text = iid

        strSQL = "Delete from BILLING_CAL where id = '" & iid & "'" 'removed from the database

        Dim i
        For i = 0 To tempds.Tables.Count - 1
            If tempds.Tables("table").Rows.Item("id") Is iid.ToString Then
                temprow.Table().Rows().RemoveAt(i)
            End If
        Next

        ds = tempds
        dgprogress.DataSource = ds
        dgprogress.DataBind()

        ExecuteDBCommand(strSQL) ' executes the delete statement

    End Sub

My problem it is not working.  What am i doing wrong.
0
Comment
Question by:WolfManBN25
7 Comments
 
LVL 18

Expert Comment

by:tusharashah
ID: 19611894
Your changes are not commited untill you call AcceptChange method.. Modify your code like following;
  tempds.AcceptChanges()
  ds = tempds
  dgprogress.DataSource = ds
  dgprogress.DataBind()
0
 

Author Comment

by:WolfManBN25
ID: 19611960
I get an error message:

Server Error in '/WebApplication1' Application.
--------------------------------------------------------------------------------

Input string was not in a correct format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.FormatException: Input string was not in a correct format.

Source Error:


Line 176:        Dim i
Line 177:        For i = 0 To tempds.Tables.Count - 1
Line 178:            If tempds.Tables("table").Rows.Item("id") Is iid Then
Line 179:                '    tempds.Tables(0).Rows(0).Delete()
Line 180:                'temprow.Delete()
 

Source File: c:\inetpub\wwwroot\WebApplication1\billing.aspx.vb    Line: 178

0
 
LVL 18

Expert Comment

by:tusharashah
ID: 19612291
try with this..
------------------------------------------------------------------------------------
For i As Integer = 0 To tempds.Tables(0).Rows.Count - 1
      If tempds.Tables(0).Rows(i)("id").ToString() = iid.ToString() Then
            tempds.Tables(0).Rows.RemoveAt(i)
      End If
Next

tempds.AcceptChanges()

ds = tempds
dgprogress.DataSource = ds
dgprogress.DataBind()
------------------------------------------------------------------------------------
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 6

Expert Comment

by:riyazthad
ID: 19612402
You are checking a dararow with a string. How many tables are there in dataset. I dont understand why u r looping thru all tables in dataset (For i = 0 To tempds.Tables.Count - 1)

i think u need to check something like  If tempds.Tables("table").Rows(0).Item("id") Is iid Then
may b in ur case u need to Rows(e.Item.ItemIndex) depends in ur condition,

u can also use simply ds.Tables(0).Rows(0)(0).ToString
0
 

Author Comment

by:WolfManBN25
ID: 19612534
my hidden id column in the datagrid/datatable is a string it is a concatination of patient number, and the date/time.  
There is one table in the dataset with many rows.  What i want to do is delete a entry in the datatable based on the hidden id column.  Then update the dataset and rebind to the datagrid.

everytime i run the web app I get an error message:

Server Error in '/WebApplication1' Application.
--------------------------------------------------------------------------------

There is no row at position 4.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IndexOutOfRangeException: There is no row at position 4.

Source Error:


Line 175:
Line 176:        For i As Integer = 0 To tempds.Tables(0).Rows.Count - 1
Line 177:            If tempds.Tables(0).Rows(i)("id").ToString() = iid.ToString() Then
Line 178:                tempds.Tables(0).Rows.RemoveAt(i)
Line 179:            End If
 

Source File: c:\inetpub\wwwroot\WebApplication1\billing.aspx.vb    Line: 177

I entered 5 pieces of sample data.
0
 
LVL 34

Expert Comment

by:Sancler
ID: 19613505
The problem is that the For i To x construct calculates the x at the start of the loop and does not recalculate it as you pass through the loop.  So, it starts off (with 5 rows) as 4.  Within the loop, the code removes one of the rows: there are now only 4 rows.  But the loop still presses on to the fifth row - that is, index 4 - because that is the maximum value for i that it was originally given.

There's all sorts of alternatives.  The simplest is probably to reverse the order of the loop

For i As Integer = tempds.Tables(0).Rows.Count - 1 To 0 Step - 1

That way, the removal of a later row does not mean that earlier rows are not still there to be visited.

Roger
0
 
LVL 18

Accepted Solution

by:
tusharashah earned 2000 total points
ID: 19614126
Roger is on target.. so is his solution,
here's alternate solution for doing the same..

For i As Integer = 0 To tempds.Tables(0).Rows.Count - 1
      If tempds.Tables(0).Rows(i)("id").ToString() = iid.ToString() Then
            tempds.Tables(0).Rows.RemoveAt(i)
            Exit For
      End If
Next
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question