I have run into an issue with system.data tables where i want to print out each row for each column, however I cant find a way that dose this for mixed data types such as the ones comprised in my table.
here is the relevant code snippet:
table.Rows[0].Field<int>(i)
Which requires a date type after the field classifier, but it breaks my code if its int or string, I need a solution that will work with both as well as bool. And here is the whole class:
public DataTable MakeTable()
{
using (DataTable table = new DataTable())
{
table.Columns.Add("ID", typeof(int));
table.Columns.Add("First Name", typeof(string));
table.Columns.Add("Surname", typeof(string));
table.Columns.Add("Permanent?", typeof(bool));
table.Columns.Add("Salary", typeof(int));
table.Columns.Add("Bonus", typeof(int));
table.Columns.Add("Day Rate", typeof(int));
table.Columns.Add("Weeks Worked", typeof(int));
table.Rows.Add(1, "Joe", "Bloggs", true, 40000, 5000, null, null);
table.Rows.Add(2, "John", "Smith", true, 45000, 2500, null, null);
table.Rows.Add(3, "Clare", "Jones", false, null, null, 350, 40);
int x = 0;
for (int i = 0; i < 8; i++)
{
Console.WriteLine(table.Columns[i].ColumnName + ": " + table.Rows[0].Field<int>(i));
Console.WriteLine();
if (i == 4)
{
x++;
if (x == 3)
{
Console.WriteLine("BREAK");
break;
}
i = -1;
}
Console.WriteLine("\n");
}
return table;
}
}
Any suggestions?
many thanks in advance been stuck on this all day with no avail.