0

我使用 cassia.dll 来管理终端服务器并编写简单的程序。我将获得 List 属性,但不知道如何在 Datarow 中进行动态添加。

DataTable 用于过滤。

 private void btn_GetTSServers_Click(object sender, EventArgs e)
        { 
        TSManager = new TerminalServicesManager();
        ITerminalServer ITS = TSManager.GetRemoteServer("localhost");
        ITS.Open();

        BSource = new BindingSource();
        DTable = new DataTable();

        Type t = ITS.GetSessions().First().GetType();
        PropertyInfo[] propinfo = t.GetProperties();

        foreach (PropertyInfo prop in propinfo)
        {
            DTable.Columns.Add(prop.Name);
        }

        foreach(ITerminalServicesSession session in ITS.GetSessions())
        {
           DTable.Rows.Add(session.ServerName, .. .. . .. etc How make Dynamic?)   
        }

        dataGridView1.DataSource = DTable;
}
        private void button1_Click(object sender, EventArgs e)
        {
            (dataGridView1.DataSource as DataTable).DefaultView.RowFilter = textBox1.Text;
        }
4

1 回答 1

0

您可以尝试类似:(伪代码,未经测试/优化)

foreach(var session in ITS.GetSessions())
{
    Type t = session.GetType();
    PropertyInfo[] propinfo = t.GetProperties();
    var list = new List<object>();
    foreach (PropertyInfo prop in propinfo)
    {
        list.Add(prop.GetValue(session, null));
    }
    DTable.Rows.Add(list.ToArray());   
}
于 2015-11-19T12:19:01.007 回答