views:

692

answers:

2

I am working on a windows form application. How do i use the find method of a datatable to find a row if the datatable has a compound key?

Table Structure Col A, Col B, Col C

Col A and Col B make up the compound key. I want to find the row where the value in Col A is 6 and Col B is 5

+1  A: 

When you "set" the Primary key of the datatable, the parameter value is an array of DataColumns...

if your datatable is in variable dt...,

dt.PrimaryKey = new DataColumn[] {dt.Columns["ColA"], dt.Columns["ColB"]};

Then pass an array of object vlaues to the Find() method

object[] keyVals = new object[] {6, 5};
DataRow dr = dt.Rows.Find(keyVals);

or, just

DataRow dr = dt.Rows.Find(new object[] {6, 5});
Charles Bretana
A: 

There is an overload that you can use to pass in two different values to the find method. Here is the MSDN doc.

So you would most likely be doing something like.

DataTable.Rows.Find(6,5)
Mitchel Sellers
Is it me or am I reading that doc wrong? It says the return value is an array of DataRow objects, which it isn't.
MusiGenesis
I meant: is it MSDN or am I reading that doc wrong?
MusiGenesis
Yes, text in MSDN is a typo... return value is a single DataRow object
Charles Bretana