[英]C# Custom bind in DataGridView (WinForm)
I have three classes我有三个班
public class FeatureBook
{
public string Id { get; set; }
public String name { get; set; }
public String type { get; set; }
}
public class Feature
{
public String feature_id { get; set; }
public String value { get; set; }
}
public class Human {
public string Id { get; set; }
public bool validated { get; set; }
public List<Feature> features { get; set; }
public override String ToString() => Id;
}
Human
has List<Feature>
. Human
有List<Feature>
。 Feature
is linked to FeatureBook
by feature_id
Feature
通过feature_id
链接到FeatureBook
And I have DataGridView.我有DataGridView。
How can I get something like in the picture:我怎样才能得到像图片中的东西:
First of all, for such of functionality, i'd use 2 datagridviews with master-detail relationship.首先,对于这样的功能,我会使用 2 个具有主从关系的数据网格视图。
Second of all, if you would like to bind data to single datagridview, you need to convert Feature
's rows into columns.其次,如果您想将数据绑定到单个 datagridview,您需要将Feature
的行转换为列。 Here is complete sample (created with LinqPad ):这是完整的示例(使用LinqPad创建):
void Main()
{
//create human list
List<Human> people = new List<Human>()
{
new Human(){Id = "H1", validated =false, features = new List<Feature>()
{
new Feature(){feature_id = "H1F1", value ="Feature 1"},
new Feature(){feature_id = "H1F2", value ="Feature 2"}
}},
new Human(){Id = "H2", validated =false, features = new List<Feature>()
{
new Feature(){feature_id = "H2F1", value ="Feature 1"},
new Feature(){feature_id = "H2F2", value ="Feature 2"},
new Feature(){feature_id = "H2F3", value ="Feature 3"},
new Feature(){feature_id = "H2F4", value ="Feature 4"},
new Feature(){feature_id = "H2F5", value ="Feature 5"}
}}
};
//create datatable
DataTable dt = new DataTable();
//add known columns (related to Human)
dt.Columns.AddRange(new DataColumn[]
{
new DataColumn("Id", typeof(string)),
new DataColumn("validated", typeof(string))
});
//get max. of futures
int fc = people.Select(x=>x.features.Count).Max();
//add columns related to Feature
for(int i=0; i<fc; i++)
dt.Columns.Add(new DataColumn($"Feature {i}"));
//add data to datatable
foreach(Human h in people)
{
//add Human details
DataRow dr = dt.NewRow();
dr["Id"] = h.Id;
dr["validated"] = h.validated;
//add Feature details
for(int i=0; i<h.features.Count; i++)
{
Feature f = h.features[i];
dr[$"Feature {i}"] = f.value;
}
dt.Rows.Add(dr);
}
//datatable is ready to use
//dump its content ;)
dt.Dump();
}
// Define other methods and classes here
public class FeatureBook
{
public string Id { get; set; }
public string name { get; set; }
public string type { get; set; }
}
public class Feature
{
public string feature_id { get; set; }
public string value { get; set; }
}
public class Human
{
public string Id { get; set; }
public bool validated { get; set; }
public List<Feature> features { get; set; }
public override string ToString() => Id;
}
Note: there's few other ways to achieve that, but i wanted to show you the simplest way;)注意:几乎没有其他方法可以实现,但我想向您展示最简单的方法;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.