Link to home
Start Free TrialLog in
Avatar of natejacobs
natejacobs

asked on

Best way to retrieve a value from a DataRow

Does anyone have a quick way to retrieve a value from a System.Data.DataRow object?
If it were the primary key I could use the Find() method, but it's not.  I have a value and I want to get the primary key for the row.  I have the name of the column  I'd rather not go into the ItemArray[].  Is there an easier way that I'm missing?
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Assuming VB.NET.  If you have:

Dim dr As System.Data.DataRow, then you can get the value from dr("PrimaryKey").

Bob
Avatar of natejacobs
natejacobs

ASKER

Bob -

It's the primary key that I'm looking for.  I have the other value.
You're losing me.  I don't understand what you are looking for.  Could you please explain more about what you would like?  

I assumed that you were looking to get a value from a DataRow.

Bob
I assume you mean that you have a datatable and you want to figure out which datarow has a column = to some value and then get the Primary Key for that row.

Use dataview

Dim dv as New DataView(datatable)
dv.Filter = "column = 'value'"
for each row as DataRow in dv.Rows
  'row("primarykey")
next
I appreciate it, Bob.

I have a System.Data.DataTable bound to a combo box.  The DataTable is called "Vendor".  It has 2 columns, "VendorID" and VendorName".  "VendorName" is bound to a combo box.  "VendorID" is not bound to anything.  When the user selects a vendor name, I need to get the VendorID.

Nate
ASKER CERTIFIED SOLUTION
Avatar of mdamico
mdamico

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Some days I just hate computers.

Thanks for the help.