Thursday 15 May 2014

C# DataSet Print Columns and Rows

//DataSet ds=(DataSet)table;
DataTable table = results.Tables[0];
string strTable = string.Empty;
foreach (DataColumn dc in table.Columns)
{
//Console.Write("{0,15}", dc.ColumnName);
strTable += dc.ColumnName + " ";
}
//Console.Write(vbCrLf);
strTable += "vbCrLf";
// Display the data for each row. Loop through the rows first.
//DataRow dr;
foreach (DataRow dr in table.Rows)
{
// Then loop through the columns for the current row.
int i;
for (i = 1; i <= table.Columns.Count; i++)
{
//Console.Write("{0,15}", dr[i-1]);
strTable += dr[i - 1] + " ";
}
// Add a line break after every row
//Console.Write(vbCrLf);
strTable += "vbCrLf";
}
//End
string k = strTable;