You may access columns in the Columns collection.
I will demonstrate the itteration using a list of strings:
List<string> columns = new List<string>();
columns .Add("price");
columns .Add("quantity");
columns .Add("bid");
columns .Add("price");
foreach(string columnName in columns)
{
if(MyTable.Columns[columnName] == null)
{
//Col does not exist so add it:
MyTable.Columns.Add(columnName);
}
}
Notice that the list has the column price
twice, but it will only add it once.
public DataTable GetDataTable()
{
var dt = new DataTable();
dt.Columns.Add("Id", typeof(string)); // dt.Columns.Add("Id", typeof(int));
dt.Columns["Id"].Caption ="my id";
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Job", typeof(string));
dt.Columns.Add("RowLabel", typeof(string));
dt.Rows.Add(GetHeaders(dt));
dt.Rows.Add(1, "Janeway", "Captain", "Label1");
dt.Rows.Add(2, "Seven Of Nine", "nobody knows", "Label2");
dt.Rows.Add(3, "Doctor", "Medical Officer", "Label3");
return dt;
}