Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find a row in a DataTable

Tags:

c#

winforms

I've a table in a DataSet and I want to search for a row in this Table using a unique key.

My question is : Is there any method that allows me to find this row without using loops ?

This is the code I wrote using the forech loop :

foreach (var myRow in myClass.ds.Tables["Editeur"].AsEnumerable())
{
     if (newKeyWordAEditeurName == myRow[1] as String)
         id_Editeur_Editeur = (int)myRow[0];
}
like image 742
Spoon Yukina Avatar asked Sep 29 '12 21:09

Spoon Yukina


People also ask

How to select particular rows from DataTable in c#?

Rows. Add(1); int insertedValue = 1; DataRow[] dr = dt. Select("ID = '" + insertedValue. toString() + "'");

What is the row selector?

The row selector is a component that acts like a visual filter for datasets. It takes one dataset, chops it up into various ranges based on its configuration, and lets the user choose the splices. Then it creates a virtual dataset that only contains the rows that match the selected splices.


1 Answers

Sure. You have the Select method off of a DataTable. GEt the table from your DataSet, and use Select to snag it.

void Demo(DataSet ds)
{
    DataTable dt = ds.Tables[0]; // refer to your table of interest within the DataSet

    dt.Select("Field = 1"); // replace with your criteria as appropriate
}
like image 192
David W Avatar answered Nov 15 '22 13:11

David W