tags:

views:

135

answers:

2
  For Each dr As myDAL.UsersRow In data
     str.AppendLine(dr.UserName)
  Next

in the above code, I also need to include the row index so that it's something like;

str.AppendLine(dr.IndexNumber & " " & dr.UserName)

How can I achive this?

P.S. data is not a datatable but a generic list of myDAL.UsersRow

+1  A: 

If data is a List<myDAL.UsersRow> as you suggest, you can use a "for" loop rather than a "for each" loop:

for i = 0 to data.Count - 1
    str.AppendLine(i & " " & data[i].UserName)
next

If, however, you're implying that your "data" list is not in the same order as the original DataTable's rows, you might be able to use the DataRowCollection.IndexOf method to locate the row in the original table:

for each dr as myDAL.UsersRow in data
    str.AppendLine(dr.Table.Rows.IndexOf(dr) & " " & dr.UserName)
next
Matt Hamilton
A: 

Declare a counter variable before the ForEach loop, and increment it inside it.

    Dim counter As Integer = 0
    For Each dr As myDAL.UsersRow In data
        counter = counter + 1
        str.AppendLine(counter.ToString() & " " & dr.UserName)
    Next
Scott Ferguson