-
Dec 16th, 2016, 06:53 PM
#1
Thread Starter
Lively Member
How do I read a datarow in a datatable?
I have a datatable and I would like to use a Message box to view data rows periodically, how do i do this?
the datatable has columns in it that will change from time to time, but i'd like the output to look like this:
FirstName, LastName, Address, City, State, Zip
Why doesn't the below work?
Code:
MsgBox(datatabel.rows(rownumber).tostring)
-
Dec 16th, 2016, 08:06 PM
#2
Re: How do I read a datarow in a datatable?
The code you have there is calling the ToString method on the DataRow. It's a bit presumptuous to assume that that will output a String contain all the row's fields in the format you want. Most complex will simply return the type name when you call ToString, or sometimes the type name and the value of the most-used property. In the case of a DataRow, you have to actually get the values from each field and then use them in whatever way is appropriate in your case. If you want to do that in a way that works regardless of the number and type of columns then you can do this:
vb.net Code:
Dim table As DataTable '... For Each row In table.AsEnumerable() MessageBox.Show(String.Join(", ", row.ItemArray)) Next
The ItemArray property returns an Object array containing all the field values. If you want code more specific to your data then you can do something like this:
vb.net Code:
Dim table As DataTable '... For Each row In table.AsEnumerable() MessageBox.Show(String.Format("{0}, {1:d}, {2:c}", row("Name"), row("DateOfPurchase"), row("Cost"))) Next
By specifying the columns you want by name or index, you can ignore some columns and also format certain columns in ways other than the default.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|