C#: How do I iterate the contents in a DataColumn?

zion picture zion · Sep 9, 2009 · Viewed 11.7k times · Source

I have a database column I simply need to check to see if a value is there.

        DataTable dt = masterDataSet.Tables["Settings"];
        DataColumn SETTINGS = dt.Columns["DEFAULTSETTINGS"];

I just need to iterate over the values in this column to see if a value exists. Help

Answer

Pierre-Alain Vigeant picture Pierre-Alain Vigeant · Sep 9, 2009

You iterate the rows, not the column

DataColumn SETTINGS = dt.Columns["DEFAULTSETTINGS"];
foreach(DataRow row in dt.Select())
{
    object columnValue = row[SETTINGS];
    // Do something with columnValue
}