C#动态设置数据行
本文关键字:置数据 动态 | 更新日期: 2023-09-27 18:22:25
我使用casia.dll来管理终端服务器并编写简单的程序。我会得到List属性,但不知道如何在Datarow中进行动态添加。
用于Filtring的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;
}
您可以尝试以下操作:(伪代码,未测试/优化)
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());
}