1

I am reading csv file data and want to Print the datatable in Console application.

Please tell me how can I do that. Below is my existing code.

  string csv_file_path = @"C:\Users\files\LP.csv";
  DataTable csvData = GetDataTabletFromCSVFile(csv_file_path);
  //Console.WriteLine(csvData);

  foreach (DataRow row in csvData.Rows)
  {
       Console.WriteLine();
       // ... Write value of first field as integer.
  }
  Console.ReadLine();
Munim Munna
  • 17,178
  • 6
  • 29
  • 58
  • Possible duplicate of [How do I extract data from a DataTable?](https://stackoverflow.com/questions/1346132/how-do-i-extract-data-from-a-datatable) – Shintu Joseph Mar 25 '18 at 06:27
  • See answers here: [Print Contents Of A DataTable](https://stackoverflow.com/questions/15547959/print-contents-of-a-datatable/) – Tawab Wakil Apr 10 '20 at 18:43

2 Answers2

1

i have created a small method which can print any Datatable (can be Improved)

    public static void printDataTable(DataTable tbl)
    {
        string line = "";
        foreach (DataColumn item in tbl.Columns)
        {
            line += item.ColumnName +"   ";
        }
        line += "\n";
        foreach (DataRow row in tbl.Rows)
        {
            for (int i = 0; i < tbl.Columns.Count; i++)
            {
                line += row[i].ToString() + "   ";
            }
            line += "\n";
        }
        Console.WriteLine(line) ;
    }
Sayed Muhammad Idrees
  • 1,245
  • 15
  • 25
0

Specify to C # the parameters you want to obtain

string csv_file_path = @"C:\Users\files\LP.csv";
DataTable csvData = GetDataTabletFromCSVFile(csv_file_path);

foreach(DataRow row in csvData.Rows)
 { 
     string name = row["name"].ToString();
     string description = row["description"].ToString();
     string icoFileName = row["iconFile"].ToString();
     string installScript = row["installScript"].ToString();
 }

Console.ReadLine();

If you need to extract data from each row then you can use

table.rows[rowindex][columnindex]

Or if you know the column name

table.rows[rowindex][columnname]

Héctor M.
  • 2,302
  • 4
  • 17
  • 35