1

I have searched high and low for a method to show the entire row of a C# datatable, both by referencing the row number and by simply writing the row contents to a string variable and showing the string in the console. I can specify the exact row and field value and display that value, but not the whole row. This is not a list in C#, this is a datatable.

For the simple code below, the output I get for the first WriteLine is "Horse", but the second two WriteLine commands, I get the console output of "System.Data.DataRow" instead of the whole row of data.

What am I doing wrong? Any help would be appreciated.

using System;
using System.Data;
using System.Threading;

namespace DataTablePractice
{
    class Program
    {
        static void Main(string[] args)
        {

            // Create a DataTable.
            using (DataTable table = new DataTable())
            {
                // Two columns.
                table.TableName = "table";
                table.Columns.Add("Number", typeof(string));
                table.Columns.Add("Pet", typeof(string));

                // ... Add two rows.
                table.Rows.Add("4", "Horse");
                table.Rows.Add("10", "Moose");

                // ... Display first field of the first row in the console
                Console.WriteLine(table.Rows[0].Field<string>(1));

                //...Display the first row of the table in the console
                Console.WriteLine(table.Rows[0]);

                //...Create a new row variable to add a third pet
                var newrow = table.Rows.Add("15", "Snake");
                string NewRowString = newrow.ToString();

                //...Display the new row of data in the console
                Console.WriteLine(NewRowString);

                //...Sleep for a few seconds to examine output
                Thread.Sleep(4000);

            }
        }
    }
}

sethjbr
  • 85
  • 1
  • 10
  • Inspect `table.Rows[0]` [in the debugger](https://idownvotedbecau.se/nodebugging/). Notice it's not any kind of string? If you want to catenate the string field values of this row you have to do that yourself; there are [many ways of doing this](https://stackoverflow.com/questions/30644853/). – Dour High Arch Sep 23 '19 at 17:38

1 Answers1

1

When you run this:

Console.WriteLine(table.Rows[0]);

It's in effect calling this:

Console.WriteLine(table.Rows[0].ToString());  // prints object type, in this case a DataRow

If it were your own class, you could override ToString to return whatever you need, but you don't have that option with the DataRow class. And so it uses the default behavior as described here:

Default implementations of the Object.ToString method return the fully qualified name of the object's type.

You could iterate through the columns, like this for example:

var row = table.Rows[0];
for (var i = 0; i < row.Count; i++)
    Console.Write(row[i] + " : ");

Or, a shorter way to print them all out:

Console.WriteLine(String.Join(" : ", table.Rows[0].ItemArray));

Given your data, maybe you just want to reference the two fields?

foreach (DataRow row in dt.Rows)
    Console.WriteLine($"You have {row[0]} {row[1]}(s).");

// You have 4 Horse(s).
// You have 10 Moose(s).
Grant Winney
  • 61,140
  • 9
  • 100
  • 152
  • Thank you very much! Then, I figured out if I use Console.WriteLine(String.Join(" ", newrow.ItemArray));, it will then write row that was created with the newrow variable to the console. It works very well. :-) – sethjbr Sep 23 '19 at 18:18