0

Basically I want to know how to select a specific column by calling its name and compare its row value to a string/int.

This is my datatable https://i.stack.imgur.com/LgYVd.png

enter image description here

Something like this: iterate through particular column in a datatable

        adp.Fill(ds); //Fill Dataset
        dt = ds.Tables[0]; //Then assign table to dt


        foreach (DataRow row in dt.Rows)
        {


        }
slugster
  • 49,403
  • 14
  • 95
  • 145
  • `if (Convert.ToInt32(row["column"]) == 1)` – VDWWD Feb 07 '19 at 10:33
  • Plenty of answers here https://stackoverflow.com/questions/13760072/select-certain-columns-of-a-data-table – Syntax Error Feb 07 '19 at 10:33
  • if it's really an `int` i'd use `bool isMyPort = row.Field("Master_Port") == yourPort`. One advantage, it supports nullables, so if this column can contain `DbNull`s you can write: `int? port = row.Field("Master_Port")` – Tim Schmelter Feb 07 '19 at 10:35

2 Answers2

0

You can use the column name to get the value as like below

int Value;
bool IsInteger;

foreach (DataRow row in dt.Rows)
{  
IsInteger = int.TryParse(row["columnname"].ToString(), out Value);

if(!IsInteger)
{
}
}
0

I'd suggest you to use DataRow.Field extension method from System.Data.DataSetExtensions package, its a generic method that does type conversion for you, also takes in column as DataColumn, column index or string column name.

foreach (DataRow dataRow in dataTable.Rows)
{
    dataRow.Field<string>("mycolumn");
}
Ram Kumaran
  • 621
  • 6
  • 12