My Technical Notes

Thursday, 13 July 2017

Visual Studio 2017: An Alternative to the DataTable Visualizer

In many environments, the DataTable Visualizer does not work because its DLL must reside in the "My Documents" folder. Unfortunately, if you do not have control of your computer, this folder could be on the network, which would stop the visualizer from working.

A simple alternative is to use PowerShell's `Out-GridView` within C#. The following class `_PS` defines the extension method `OutGridView` which will invoke `Out-GridView`:


using System;
using System.Collections;
using System.Data;

public static class _PS
{
    public static void OutGridView(this DataTable t)
    {
        t.Rows.OutGridView();
    }

    public static void OutGridView(this IEnumerable t)
    {
        using (var ps = System.Management.Automation.PowerShell.Create())
        {
            ps.Commands.AddCommand("Out-GridView");
            var result = ps.Invoke(t);
        }
    } 
}

For instance, if we had a `DataTable` `dt`, then in the Intermediate/QuickWatch/Watch window, you can evaluate:


dt.OutGridView()

To use the above code, install the `System.Management.Automation` NuGet package.

No comments: