C#动态设置DataRow

时间:2015-11-19 05:46:28

标签: c# cassia

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

DataTable用于Filtring。

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

1 个答案:

答案 0 :(得分: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());   
}