PSObject直接绑定到Asp.net GridView控件

时间:2018-10-08 07:12:00

标签: asp.net powershell

我们可以直接将脚本返回的psobject绑定到asp.net gridview。

当前我的代码如下:

using (PowerShell PowerShellInstance = PowerShell.Create())
{
    PowerShellInstance.AddCommand(@"<path to script>");
        var results = PowerShellInstance.Invoke();
        if (results.Count() > 0)
        {
             DataTable tempTable = new DataTable();
             tempTable.Columns.Add("Name");
             tempTable.Columns.Add("CollectionName");
             foreach (var psObject in results)
             {
                 DataRow dr = tempTable.NewRow();
                 dr["Name"] = psObject.Properties["Name"].Value.ToString();
                 dr["CollectionName"] = psObject.Properties["CollectionName"].Value.ToString();
                 tempTable.Rows.Add(dr);    
             }
             GridView1.DataSource = results;
         GridView1.DataBind();
        }
}

是否还有其他绑定方法,所以我不必遍历PsObject的每一行。

0 个答案:

没有答案