Test if a value exists in a DataTable Column?

M

MattB

I know I can use dataviews and more looping to do this, but I'm
wondering if there's a more elegant or more concise way to do this.

I've got two DataTables with a column called guest_no. I'd love to be
able to use something like the .IndexOf method of arrays and bound
controls. Somehting like:

Dim r as DataRow
For Each r in DataTable2.Rows
If DataTable1.Columns("guest_no").IndexOf(r("guest_no")) <> -1
'record exists
'do something
Else
'record doesn't exist
'do something else
End If
End For

Is there anything like this? Obviously, the code above doesn't work.

Thanks,
Matt
 
M

Marina

A column in a datatable is like a column in a table in your database. You
wouldn't expect the column definition to just have a value, would you? No.
You would expect a column (cell) in a particular row to have a value. Same
goes here.

Asking a column to have a value doesn't make sense semantically.

Something like:

DataTable1.Rows(0)("guest_no")

makes sense. That asks for the value of the guest_no column in the first
row.

Remember that this returns an Object, turn Option Strict On, and then type
this to the correct type - in your case a string.
 
M

MattB

Yes, I know. I was just looking for a shortcut for more concise code.
Something like how I can have an array and use the IndexOf method to
quickly know if a values exists and in what element if it does without
having to loop through the array, testing each element explicitly.

Thanks for the reply,
Matt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,187
Latest member
RosaDemko

Latest Threads

Top