Sometimes I will be at a breakpoint in my code and I want to view the contents of a datatable variable (or a datatable in a dataset). The quick watch doesn't give you a very clear view of the contents. How can I view them easily?
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 | ---------------------|----------------------|----------------------|-