473,320 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Test if a value exists in a DataTable Column?

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
Nov 19 '05 #1
2 7949
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.

"MattB" <so********@yahoo.com> wrote in message
news:3t************@individual.net...
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

Nov 19 '05 #2
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

Marina wrote:
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.

"MattB" <so********@yahoo.com> wrote in message
news:3t************@individual.net...
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


Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Woody Splawn | last post by:
Lets say I have a winform that is populated with a dataset. The dataset and data table may have several rows in it. Lets say I am looking at the winform and I want to assign a value to a certain...
5
by: JC Voon | last post by:
Hi: How to reset the autoincrement value generated by DataTable ? I've master and detail table, the detail table has a autoincrement column, each time i add a new master record, i need to...
0
by: Matt | last post by:
I derived my own custom class from the datagrid class. I overrode the ProcessCmdKey Function, like this, to catch the up and down arrow keys: ====== Protected Overrides Function...
3
by: Niyazi | last post by:
Hi all, I have a dataTable that contains nearly 38400 rows. In the dataTable consist of 3 column. column 1 Name: MUHNO column 2 Name: HESNO Column 3 Name: BALANCE Let me give you some...
3
by: RSH | last post by:
Hi, I have a situation where I have two datareaders, and I want to make sure any given field from Datareader A exists in Datareader B before I can do anything with that column. I tried the code...
2
by: RSH | last post by:
Hi, Iam struggling with an application where I am trying to transfer a datarow from one sql server to another instance of sql server. The schmeas may be slightly different and I am getting an...
0
by: neeraj | last post by:
Hi Everybody Is any body suggest me how can I get all cell value together(without looping), from single column of datatable in asp.net like this "Datatable.row (0).ItemArray" This returns all...
0
by: Maart_newbie | last post by:
Hi all, I've got a question about returning the value of a pk-column to a DataTable after inserting a row (via a data-adapter) using MySql5. Here is the SQL and code concerned: ...
3
by: =?Utf-8?B?UmljaCBIdXRjaGlucw==?= | last post by:
I'm not really sure how to ask this question because I'm still getting my feet wet with data access and VB.NET, but here goes: To start off with, I'm using VB 2005 Express to connect to an Access...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.