Wednesday, August 20, 2008

DataTable C# Primary Key column .Contains

I needed to do something like use DataTable in C#, set Primary Key column, and later on maybe a "contains" ... to see if my set of ID is inside the DataTable
Anyways: this is how I did it:





DataTable myFilteredTerms = objSomethn.getresultsinDT ...
DataColumn[] myPrimaryKeyColumn = new DataColumn[1];
myPrimaryKeyColumn.SetValue(myFilteredTerms.Columns[0], 0);
myFilteredTerms.PrimaryKey = myPrimaryKeyColumn;

...

if (myFilteredTerms.Rows.Contains(intTheIdYouAreLookingFor))
{
...
}





(first column is the one with the ID-s ...)

as always, if you got pro or contra ... tell me bout it

1 comment:

Edwin Jose said...

Hi,
Thanks, this code helped me in solving my problem.