I'm having a brain fart trying to make the following method more generic such that any List<T>
can be passed in for the columnValues
parameter. Here's what I have:
public static DataRow NewRow(this DataTable dataTable, List<string> columnValues)
{
DataRow returnValue = dataTable.NewRow();
while (columnValues.Count > returnValue.Table.Columns.Count)
{
returnValue.Table.Columns.Add();
}
returnValue.ItemArray = columnValues.ToArray();
return returnValue;
}
I could change it to a List<object>
and convert the original list prior to passing it to the method but I'm sure there is a better option :-)
Edit:
Frank's post made me rethink this. In most cases that source List<T>
would be a List<object>
since the column values will most likely be different types.
For my initial use a List<string>
made sense because I was creating a dataset from a CSV parse which is all text at that point.