开发者

Show output of sp_who2 in WPF datagrid

开发者 https://www.devze.com 2023-02-07 20:21 出处:网络
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 -

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(S开发者_C百科TR_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?


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

this.dataGrid1.ItemsSource = dt.DefaultView; 


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

0

精彩评论

暂无评论...
验证码 换一张
取 消