delete row from DataSet??

  • Thread starter Thread starter michaeldcummings
  • Start date Start date
M

michaeldcummings

I have been reading a bunch of posts about how to delete rows from a
dataset and have yet to find an answer that doesnt also have something
to do with using datagrids.

I am just trying to remove a row from a dataset that I have, but I cant
figure out how to get the row index value so that I can call:

DS.Tables("table").Rows(row).Delete()

how do I get the value of "row"?? it seems like there should be a very
simple call to get the index but I cant find one...

Here is generally what I am trying do do...

'----------------------------------------------------------------

Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnDel.Click
Dim dr As DataRow
Dim row As Integer
Try

If radExisting.Checked = True Then
For Each dr In cattlelistDS.Tables("AnimalList").Rows
If dr("Animal") = lstList.SelectedItem Then

'get the row index
row = ???????

cattlelistDS.Tables("animallist").Rows(row).Delete()

End If
Next
End If

lstList.Items.Remove(lstList.SelectedItem)
lblCount.Text = "Animals: " & lstList.Items.Count

Catch
Exit Sub

End Try

End Sub
'----------------------------------------------------------------




Thanks for any help
~Mike
 
You don't need the row index as you already have the DataRow object, which is
what you call Delete on. However, your code's not gonna work because you
cannot modify the collection when using for each. Use a For Next loop.

for i as integer = 0 to Table.rows
if condition = true then
Table.Rows.Delete();
end if
next
 
Ok.... still having problems.
I am new to working with xml and datasets together and Im working on a
project that requires me to:
1. load a list from the xml document into a listbox
2. if the user adds an item to the listbox then when they hit save it
should just append that record to the xml file
3. if the user deletes and item it should update the dataset that I
generated from the xml file, delete the xmlfile, and rewrite the xml
file so that the deleted data is no longer included.

I am doing this on .netCF with VB.net. It seems like this should be an
easy task.

Ron I know you said I dont need to have the index, but i tried your
code and it doesnt run either(converted to vb of course).

this is what my xml file looks like:
------------------------------------
<?xml version="1.0" standalone="yes"?>
<CattleList>
<AnimalList>
<ListName>johnny</ListName>
<Animal>r2d2</Animal>
</AnimalList>
<AnimalList>
<ListName>johnny</ListName>
<Animal>c3p0</Animal>
</AnimalList>
</CattleList>
-------------------------------------

this generates a dataset like this:
-------------------------------------
ListName Animal
johnny r2d2
johnny c3p0
-------------------------------------

This is just a basic dataset and I am having trouble returning any row
index
so that i could remove c3p0 (he could be index 74 out of 500)

I even tried searching for a composite key by passing an object that
contained both the listname and animal. It throws an exception that i
catch and display a message in the listbox with...
-------------------------------------
Private Sub btnDel_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnDel.Click
Dim dr As DataRow
Dim dt As DataTable

Dim dvw As DataView
Dim row As Integer
Dim arrValues(2) As Object
dt = cattlelistDS.Tables("AnimalList")



Try

If radExisting.Checked = True Then
'create dataview
dvw = cattlelistDS.Tables("AnimalList").DefaultView
'dvw.Sort = "ListName, Animal"



'find index of row
arrValues(0) = txtNewName.Text
arrValues(1) = lstList.SelectedItem

row = dvw.Find(arrValues)

If row <> -1 Then

cattlelistDS.Tables("animallist").Rows(row).Delete()
End If



End If

lstList.Items.Remove(lstList.SelectedItem)
lblCount.Text = "Animals: " & lstList.Items.Count

Catch

lstList.Items.Add("Error occured removing")
Exit Sub

End Try

End Sub
------------------------------------------


Ok... I dont know how much more detail i can go into about this, but it
is imperative that I get this working somehow....Any help is going to
be very greatly appreciated.
 
I think i may have fixed my problem...

If anyone sees a problem with this then i'd appreciate any suggestions,
but I havent broken the code yet. To anyone who has had the same
problem... here is my solution.

------------------------------------------------------------------------
Dim i = 0
If radExisting.Checked = True Then
For Each dr In cattlelistDS.Tables("AnimalList").Rows

If lstList.SelectedItem = dr("Animal") Then


cattlelistDS.Tables("animallist").Rows(i).Delete()
lblds.Text =
cattlelistDS.Tables("AnimalList").Rows.Count


Exit For

End If
i = i + 1
'cattlelistds.Tables("AnimalList").Rows.Count.
Next
End If
------------------------------------------------------------------------
 
Back
Top