Show output of sp_who2 in WPF datagrid

396 Views Asked by At

I wish to display the output of the SQL Server command "sp_who2 active" in a WPF datagrid. I've come up with the following code -

private void GetActiveSQLIds()
    {
        SqlConnection con = new SqlConnection(STR_DataSource);

        con.Open();

        SqlCommand cmd = new SqlCommand("EXEC sp_who2 active", con);

        SqlDataReader dr = cmd.ExecuteReader();

        DataTable dt = new DataTable();

        dt.Load(dr);

        this.dataGrid1.AutoGenerateColumns = true;
        this.dataGrid1.ItemsSource = dt.Select();

        con.Close();
    }

It executes ok, but actually displays the columns "RowError", "RowState" etc, rather than the output of sp_who2.

Anyone know how to do what I want to accomplish?

2

There are 2 best solutions below

0
Craig Schwarze On BEST ANSWER

Found it - just needed to change the second last line to -

this.dataGrid1.ItemsSource = dt.DefaultView; 
0
Prince Ashitaka On

this.dataGrid1.ItemsSource = (dt as IEnumerable);