How can I easily view the contents of a datatable or dataview in the immediate window
The Visual Studio debugger comes with four standard visualizers. These are the text, HTML, and XML visualizers, all of which work on string objects, and the dataset visualizer, which works for DataSet, DataView, and DataTable objects.
To use it, break into your code, mouse over your DataSet, expand the quick watch, view the Tables, expand that, then view Table[0] (for example). You will see something like {Table1} in the quick watch, but notice that there is also a magnifying glass icon. Click on that icon and your DataTable will open up in a grid view.
To beautify adinas's debugger output I made some simple formattings:
public void DebugTable(DataTable table)
{
Debug.WriteLine("--- DebugTable(" + table.TableName + ") ---");
int zeilen = table.Rows.Count;
int spalten = table.Columns.Count;
// Header
for (int i = 0; i < table.Columns.Count; i++)
{
string s = table.Columns[i].ToString();
Debug.Write(String.Format("{0,-20} | ", s));
}
Debug.Write(Environment.NewLine);
for (int i = 0; i < table.Columns.Count; i++)
{
Debug.Write("---------------------|-");
}
Debug.Write(Environment.NewLine);
// Data
for (int i = 0; i < zeilen; i++)
{
DataRow row = table.Rows[i];
//Debug.WriteLine("{0} {1} ", row[0], row[1]);
for (int j = 0; j < spalten; j++)
{
string s = row[j].ToString();
if (s.Length > 20) s = s.Substring(0, 17) + "...";
Debug.Write(String.Format("{0,-20} | ", s));
}
Debug.Write(Environment.NewLine);
}
for (int i = 0; i < table.Columns.Count; i++)
{
Debug.Write("---------------------|-");
}
Debug.Write(Environment.NewLine);
}
Best of this solution: You don't need Visual Studio! Here my example output:
SELECT PackKurz, PackName, PackGewicht FROM verpackungen PackKurz | PackName | PackGewicht | ---------------------|----------------------|----------------------|- BB205 | BigBag 205 kg | 205 | BB300 | BigBag 300 kg | 300 | BB365 | BigBag 365 kg | 365 | CO | Container, Alteru... | | EP | Palette | | IBC | Chemikaliengefäß ... | | lose | nicht verpackungs... | 0 | ---------------------|----------------------|----------------------|-
What I do is have a static class with the following code in my project:
#region Dataset -> Immediate Window
public static void printTbl(DataSet myDataset)
{
printTbl(myDataset.Tables[0]);
}
public static void printTbl(DataTable mytable)
{
for (int i = 0; i < mytable.Columns.Count; i++)
{
Debug.Write(mytable.Columns[i].ToString() + " | ");
}
Debug.Write(Environment.NewLine + "=======" + Environment.NewLine);
for (int rrr = 0; rrr < mytable.Rows.Count; rrr++)
{
for (int ccc = 0; ccc < mytable.Columns.Count; ccc++)
{
Debug.Write(mytable.Rows[rrr][ccc] + " | ");
}
Debug.Write(Environment.NewLine);
}
}
public static void ResponsePrintTbl(DataTable mytable)
{
for (int i = 0; i < mytable.Columns.Count; i++)
{
HttpContext.Current.Response.Write(mytable.Columns[i].ToString() + " | ");
}
HttpContext.Current.Response.Write("<BR>" + "=======" + "<BR>");
for (int rrr = 0; rrr < mytable.Rows.Count; rrr++)
{
for (int ccc = 0; ccc < mytable.Columns.Count; ccc++)
{
HttpContext.Current.Response.Write(mytable.Rows[rrr][ccc] + " | ");
}
HttpContext.Current.Response.Write("<BR>");
}
}
public static void printTblRow(DataSet myDataset, int RowNum)
{
printTblRow(myDataset.Tables[0], RowNum);
}
public static void printTblRow(DataTable mytable, int RowNum)
{
for (int ccc = 0; ccc < mytable.Columns.Count; ccc++)
{
Debug.Write(mytable.Columns[ccc].ToString() + " : ");
Debug.Write(mytable.Rows[RowNum][ccc]);
Debug.Write(Environment.NewLine);
}
}
#endregion
I then I will call one of the above functions in the immediate window and the results will appear there as well. For example if I want to see the contents of a variable 'myDataset' I will call printTbl(myDataset). After hitting enter, the results will be printed to the immediate window