开发者

Show output of sp_who2 in WPF datagrid

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜