1

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?

Craig Schwarze
  • 11,367
  • 15
  • 60
  • 80

2 Answers2

1

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

this.dataGrid1.ItemsSource = dt.DefaultView; 
Craig Schwarze
  • 11,367
  • 15
  • 60
  • 80
0

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

Prince Ashitaka
  • 8,623
  • 12
  • 48
  • 71