Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I easily view the contents of a datatable or dataview in the immediate window

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?

like image 547
adinas Avatar asked Jan 29 '09 13:01

adinas


People also ask

How do you view data in a DataTable?

You can access the contents of a DataTable by using the Rows and Columns collections of the DataTable. You can also use the Select method to return subsets of the data in a DataTable according to criteria including search criteria, sort order, and row state.

How do I access DataView rows?

You can access the DataRow that is exposed by the DataRowView through the Row property of the DataRowView. When you view values by using a DataRowView, the RowStateFilter property determines which row version of the underlying DataRow is exposed.

How can I see DataTable values while debugging in Visual Studio?

set the break point on the dataset/datatable(f9 shortcut key for break point) and run your application (f5 is the shortcutkey ) When the break point comes mouse hover the dataset/datatable click on the glass shown in the hover image in visual studio . Note : check compilation debug="true" is true in web config .

What is DataTable DataView?

A DataView enables you to create different views of the data stored in a DataTable, a capability that is often used in data-binding applications. Using a DataView, you can expose the data in a table with different sort orders, and you can filter the data by row state or based on a filter expression.


2 Answers

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.

enter image description here

like image 59
Rob Prouse Avatar answered Sep 22 '22 21:09

Rob Prouse


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                    |  ---------------------|----------------------|----------------------|- 
like image 27
Luluha Avatar answered Sep 22 '22 21:09

Luluha