在WPF数据网格中显示sp_who2的输出

在WPF数据网格中显示sp_who2的输出

问题描述:

我希望在WPF数据网格中显示SQL Server命令“sp_who2 active”的输出。我想出了下面的代码 -在WPF数据网格中显示sp_who2的输出

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(); 
    } 

它执行好了,但实际显示的列“RowError”,“RowState的”等,而不是sp_who2输出。

任何人都知道如何去做我想完成的事情?

+0

您是否尝试过`this.dataGrid1.ItemsSource = dt`? – decyclone 2011-01-31 05:21:49

发现它 - 只是需要改变倒数第二行 -

this.dataGrid1.ItemsSource = dt.DefaultView; 

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