I have a DataTable
which is populated by a csv file, with 8 columns and approximately 2000 rows. I wish to populate an object with my csv values that are currently stored in my DataTable
How do I index a specific DataTable
cell? In other words, I wish to treat the data table in a similar way you would a 2D array, like so:
string value = array[i][j];
Here is my code:
DataTable d = GetDataTableFromCSVFile(file);
for (int i = 0; i < d.Rows.Count; i++)
{
for (int j = 0; j < d.Columns.Count; j++)
{
//string x = d[i][j]; <-- something like this.
}
}
like this
string x = d.Rows[i][j].ToString()
The best way to iterate the DataTable
.
Foreach
works faster then for
loop:
foreach (DataRow dtRow in dtTable.Rows)
{
foreach(DataColumn dc in dtTable.Columns)
{
var field1 = dtRow[dc].ToString();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With