简体   繁体   中英

Getting specific row item from a datatable

I've got a datatable. Lets call it dt. It has three columns. id, link_id and value. I'm using a for each loop to loop through each result in the datatable.

For Each Row As DataRow In dt.Rows
    Try
        'do whatever
    Catch Ex As Exception
    End Try

In my do whatever bit, how would I grab just the value of the current row and assign it to a variable?

Try this if you are using a generic DataTable :

For Each Row As DataRow In dt.Rows
  Try
    'Show all 3 fields/columns
    Console.WriteLine Row.Item(0) & " - " & Row.Item(1) & " - " & Row.Item(2) 
  Catch Ex As Exception
  End Try
Next

Try this if you are using a strongly-typed DataTable :

For Each Row As StronglyTypedDataRow In dt.Rows
  Try
    'Show all 3 fields/columns
    Console.WriteLine Row.id & " - " & Row.link_id  & " - " & Row.Value 
  Catch Ex As Exception
  End Try
Next

You can try following.

For Each Row As DataRow In dt.Rows
            Try
               var linkId = row("link_id")
            Catch Ex As Exception
            End Try

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM