I have a string something like this:
"X","Y","Z"
"X2","Y2","Z2"
i.e, Multiple rows seperated by a new line containing multiple different string values delimited by commas.
How could I create a function to input these values into a datatable like this:
"X" "Y" "Z"
"X2" "Y2" "Z2"
for any number of rows and columns in my string?
Here's what I have so far:
public static DataTable writeToDT(string data) //Write to datatable from string
{
DataTable dataTable = new DataTable();
foreach (string header in csvHeader)
{
dataTable.Columns.Add(header);
}
using (StringReader reader = new StringReader(data))
{
string line;
while ((line = reader.ReadLine()) != null)
{
DataRow dataRow = dataTable.NewRow();
}
}
return dataTable;
}
Where csvheader
is an array already filled with the headers in my data.
I believe this creates an empty datatable with the right amount of rows and columns, but I'm not sure how to fill it with the string data I have.
Thanks in advance.