• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 522
  • Last Modified:

Move to First Row in a DataSet

Is it possible to MOVE to the First Row in a DataSet?

If this is the first row:  DataSet1.Tables(0).Rows(0)   . . .
how do I get the dataset to "move" there?

this didn't work:
DataSet1.Tables(0).Select("DataSet1.Tables(0).Rows(0)")
0
MikeMCSD
Asked:
MikeMCSD
  • 5
  • 3
  • 2
  • +1
3 Solutions
 
Éric MoreauSenior .Net ConsultantCommented:
Hi MikeMCSD,

There is not Positionning/CurrentRow in a dataset. It is only a container. The only thing I can thing of is for you to move your row into a DataRow object

Cheers!
0
 
wtconwayCommented:
As far as I know you don't really MOVE to that row. You can declare an object of that row. You can even just reference it like so:

DataSet1.Tables(0).Rows(0)

What context are you trying to do this in?
0
 
vadim63Commented:
You can get any value from your dataset:
MsgBox(DataSet1.Tables(0).Rows(0).Item(i).ToString())
where "i" is a column index
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
MikeMCSDAuthor Commented:
thanks all . .
what I'm trying to do is delete the current row, then add a row, then continue
looping through the current dataset:

For Each row In DataSet1.Tables(0).Rows
     .....
     row.Delete()
     DataSet1.Tables(0).Rows.Add(row1)
     
Next    <<<<<<<<<  get an error when I try to move to the next row . .
                                so I need to move the row up or something . . .  
0
 
wtconwayCommented:
Or if you know the name of the column:

DataSet1.Tables(0).Rows(0)("ColumnName").ToString()

that's generally how I use it.

But if you are setting the column's value to a variable, make sure you check for null first. It's a bit different in ADO.NET:

If DataSet1.Tables(0).Rows(0).IsNull("ColumnName") = False Then
   myVar = DataSet1.Tables(0).Rows(0)("ColumnName").ToString()
End If
0
 
wtconwayCommented:
The reason you're getting the error is because you're attempting to loop through the rows collection but you're changing the collection when you delete a row. So .NET throws up that error letting you know you can't do that. I got around this my moving the rows that I didn't want to delete to another datatable. Then using that datatable as my final copy.
0
 
MikeMCSDAuthor Commented:
wt . . would you have a sample of the that code?  thanks
0
 
vadim63Commented:
Why you can't just delete all unnecessary rows first and then to add the same qty of the new rows?
0
 
wtconwayCommented:
I may have a sample somewhere on EE. Let me search for it.
0
 
wtconwayCommented:
I know i have an example on EE but I can't find it. I just re-wrote it. It's not that complex, but it ASSUMES a lot. It uses the Copy() method of the datatable class to copy both structure and data to a new table that will replace your table. The only problem with that is I assume that .NET will copy the rows in the EXACT same order as the original table. I've never done any reading on this so I can't be sure.

    Public Function DeleteRows(ByVal tbl As DataTable) As DataTable
        Dim ret As DataTable = tbl.Copy()
        Dim i As Integer
        For i = 1 To tbl.Rows.Count
            Dim row As DataRow = tbl.Rows(i - 1)
            If row("StoreNumber") = "0000" Then
                ret.Rows.RemoveAt(i - 1)
            End If
        Next
        Return ret
    End Function

Now in my example I just test the StoreNumber column and if it's "0000" then I remove it. You could use this function like so:

Dim myTable as DataTable 'you know, set it to whatever it should be
myTable = DeleteRows(myTable)

and it will remove the rows from the "copy" and bring the copy back to you as the original, follow me?
0
 
MikeMCSDAuthor Commented:
I'll have to come back to this one . . thanks all
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now