Link to home
Start Free TrialLog in
Avatar of WolfManBN25
WolfManBN25

asked on

delete datarow from datatable in datagrid

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.
Avatar of tusharashah
tusharashah

Your changes are not commited untill you call AcceptChange method.. Modify your code like following;
  tempds.AcceptChanges()
  ds = tempds
  dgprogress.DataSource = ds
  dgprogress.DataBind()
Avatar of WolfManBN25

ASKER

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

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()
------------------------------------------------------------------------------------
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
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.
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
ASKER CERTIFIED SOLUTION
Avatar of tusharashah
tusharashah

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial