简体   繁体   中英

C# Dynamic Set DataRow

I used cassia.dll for manage terminal servers and write simple program. I'll get List Property but not know how make dynamic add in Datarow.

DataTable use for 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;
        }

You could try something like: (pseudo-code, not tested/optimized)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM