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?
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.
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.
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 .
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.
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 | ---------------------|----------------------|----------------------|-
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With