Steam
Steam

Reputation: 9856

Selecting a row from a particular column datatable without LINQ

My DataTable has the columns - Id, Name, Address. I need to select the column Address only WHERE ID = 7. How do I do this ? No LINQ please.

I was thinking of this -

DataView view = new DataView(MyDataTable);
DataTable distinctValues = view.ToTable(true, "ColumnA");
Now you can select.

DataRow[] myRows = distinctValues.Select();
//Get the desired answer by iterating myRows.

Is there a simpler way ?

thanks.

Upvotes: 0

Views: 540

Answers (1)

Selman Genç
Selman Genç

Reputation: 101681

Well if you don't want to use LINQ, you can use a simple foreach loop:

DataTable distinctValues = view.ToTable(true, "ColumnA");
var myRows = new List<DataRow>();
foreach(DataRow row in distinctValues.Rows)
{
   if(row["Id"].ToString() == "7") myRows.Add(row);
}

Upvotes: 1

Related Questions