How can I find a DataRow in a DataSet by it's PrimaryKey value?(C++/CLI)

G

google

How can I find a DataRow in a DataSet by it's PrimaryKey value? This
would be similar to an SQL statement such as "SELECT * FROM MyTable
WHERE MyIndex = 2"...

In the following code I've tried to do this but "dataSet-
Tables["GpsData"]->Rows->Find" returns a nullptr every time...

/* C++/CLI Code Sample (doesn't work) */

cli::array<System::Data::DataColumn ^> ^colarray =
gcnew cli::array<System::Data::DataColumn ^>(1);
colarray[0] =
dataSet->Tables["GpsData"]->Columns["Distance"];
dataSet->Tables["GpsData"]->PrimaryKey =
colarray;

DataRow ^drow =
dataSet->Tables["GpsData"]->Rows->Find(
dataSet->Tables["CisData"]->Rows["GpsIndexFK"]
);

if(drow != nullptr)
System::Windows::Forms::MessageBox::Show(
safe_cast<Int32 ^>(drow["Index"])->ToString()
);
 
C

Carl Daniel [VC++ MVP]

How can I find a DataRow in a DataSet by it's PrimaryKey value? This
would be similar to an SQL statement such as "SELECT * FROM MyTable
WHERE MyIndex = 2"...

Use DataTable.Select - you can pass a filter string (basically, the
expression from the Where clause of your SQL) to select the rows you want.
You'll get back an array of DataRows.

-cd
 
G

google

Use DataTable.Select - you can pass a filter string (basically, the
expression from the Where clause of your SQL) to select the rows you want.
You'll get back an array of DataRows.

-cd

You're a life saver. I'd actually already wrote a function to scan a
column for a value because I hadn't realized this method existed.

Thanks
 

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

Top