[c#] How do I get column names to print in this C# program?

I've cobbled together a C# program that takes a .csv file and writes it to a DataTable. Using this program, I can loop through each row of the DataTable and print out the information contained in the row. The console output looks like this:

--- Row ---
Item: 1
Item: 545
Item: 507
Item: 484
Item: 501

I'd like to print the column name beside each value, as well, so that it looks like this:

--- Row ---
Item: 1   Hour
Item: 545 Day1 KW
Item: 507 Day2 KW
Item: 484 Day3 KW
Item: 501 Day4 KW

Can someone look at my code and tell me what I can add so that the column names will print? I am very new to C#, so please forgive me if I've overlooked something.

Here is my code:

// Write load_forecast data to datatable.
DataTable loadDT = new DataTable();
StreamReader sr = new StreamReader(@"c:\load_forecast.csv");                      

string[] headers = sr.ReadLine().Split(',');
foreach (string header in headers)
{
    loadDT.Columns.Add(header); // I've added the column headers here.
}

while (sr.Peek() > 0)
{
    DataRow loadDR = loadDT.NewRow();
    loadDR.ItemArray = sr.ReadLine().Split(',');
    loadDT.Rows.Add(loadDR);
}

foreach (DataRow row in loadDT.Rows)
{
    Console.WriteLine("--- Row ---");
    foreach (var item in row.ItemArray)
    {
        Console.Write("Item:");
        Console.WriteLine(item); // Can I add something here to also print the column names?
    }
}

This question is related to c# datatable datarow datacolumn

The answer is


You need to loop over loadDT.Columns, like this:

foreach (DataColumn column in loadDT.Columns)
{
    Console.Write("Item: ");
    Console.Write(column.ColumnName);
    Console.Write(" ");
    Console.WriteLine(row[column]);
}

foreach (DataRow row in dt.Rows)
{
    foreach (DataColumn column in dt.Columns)
    {
        ColumnName = column.ColumnName;
        ColumnData = row[column].ToString();
    }
}

Print datatable rows with column

Here is solution 

DataTable datatableinfo= new DataTable();

// Fill data table 

//datatableinfo=fill by function or get data from database

//Print data table with rows and column


for (int j = 0; j < datatableinfo.Rows.Count; j++)
{
    for (int i = 0; i < datatableinfo.Columns.Count; i++)    
        {    
            Console.Write(datatableinfo.Columns[i].ColumnName + " ");    
            Console.WriteLine(datatableinfo.Rows[j].ItemArray[i]+" "); 
        }
}


Ouput :
ColumnName - row Value
ColumnName - row Value
ColumnName - row Value
ColumnName - row Value

Code for Find the Column Name same as using the Like in sql.

foreach (DataGridViewColumn column in GrdMarkBook.Columns)  
                      //GrdMarkBook is Data Grid name
{                      
    string HeaderName = column.HeaderText.ToString();

    //  This line Used for find any Column Have Name With Exam

    if (column.HeaderText.ToString().ToUpper().Contains("EXAM"))
    {
        int CoumnNo = column.Index;
    }
}

You can access column name specifically like this too if you don't want to loop through all columns:

table.Columns[1].ColumnName


Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to datatable

Can't bind to 'dataSource' since it isn't a known property of 'table' How to get a specific column value from a DataTable in c# Change Row background color based on cell value DataTable How to bind DataTable to Datagrid Find row in datatable with specific id Datatable to html Table How to Edit a row in the datatable How do I use SELECT GROUP BY in DataTable.Select(Expression)? How to fill a datatable with List<T> SqlBulkCopy - The given value of type String from the data source cannot be converted to type money of the specified target column

Examples related to datarow

Find row in datatable with specific id DataColumn Name from DataRow (not DataTable) How to add new DataRow into DataTable? Simple way to copy or clone a DataRow? get index of DataTable column with name Getting datarow values into a string? DataRow: Select cell value by a given column name Check if DataRow exists by column name in c#? How do I get column names to print in this C# program? C# DataRow Empty-check

Examples related to datacolumn

DataColumn Name from DataRow (not DataTable) How To Change DataType of a DataColumn in a DataTable? How do I get column names to print in this C# program?