For example I have 3 entities : product, tool, device. All of them have “id” and “Name” attributes. Now I try to write a method to fill entity’s data into a combobox. But I don’t now how to create a method that can pass any entity into it. Something like code below :
private comboBox FillCombobox(List<EntityType>)
{
using (dowacodbEntities dowacodbEntities = new dowacodbEntities())
{
comboBox comboBox = new comboBox();
List<EntityType> EntityList = dowacodbEntities.EntityType.ToList();
DataTable dt = new DataTable();
dt.Columns.Add("id");
dt.Columns.Add("Name");
dt.Rows.Add(-1, "Choose one option");
foreach (EntityType Entity in EntityList)
{
dt.Rows.Add(Entity.id, Entity.Name);
}
comboBox.ValueMember = dt.Columns[0].ColumnName;
comboBox.DisplayMember = dt.Columns[1].ColumnName;
comboBox.DataSource = dt;
return combobox;
}
}
Which EntityType can be product, tool or device.
Define interface:
Implement that interface in your
Product,ToolandDeviceclasses (it can be implemented by your partial part if entities are autogenerated) and define your method as: