本文介绍了如何在Winforms中向Datagridview显示Foreach数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有这样的foreach循环
suppose i have a foreach loop like this
foreach (String T in humans)
{
Person obj = new Person
{
Name = T.Name,
Age=T.Age,
Sex=T.Sex,
};
}
我希望上面的数据显示在datagridView上,请回答..
I want the above data to be displayed on datagridView , pls answer..
推荐答案
private DataTable ConvertCollectionToDataTable(IRegisteredTaskCollection tasks)
{
DataTable dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Status", typeof(string));
dt.Columns.Add("Triggers", typeof(string));
dt.Columns.Add("NextRunTime", typeof(DateTime));
dt.Columns.Add("LastRunTime", typeof(DateTime));
dt.Columns.Add("Author", typeof(string));
dt.Columns.Add("Created", typeof(DateTime));
foreach (IRegisteredTask T in tasks)
{
DataRow dr = dt.NewRow();
dr["Name"] = T.Name;
dr["Status"] = T.Enabled;
dr["Triggers"] = T.Definition.Triggers[1].Id;
dr["NextRunTime"] = T.NextRunTime;
dr["LastRunTime"] = T.LastRunTime;
dr["Author"] = T.Definition.RegistrationInfo.Author;
dr["Created"] = T.Definition.RegistrationInfo.Date;
dt.Rows.Add(dr);
}
return dt;
}
然后执行类似
dataGridView1.DataSource = ConvertCollectionToDataTable(tasks);
这篇关于如何在Winforms中向Datagridview显示Foreach数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!