我创建了一些代码,将 Linq.Tables (dc.GTMD_Financials) 中的数据添加到 UserControl。对于数据库中的每个条目,它都会显示一个新的用户控件。
但是我想在一个方法中使用这个代码来在整个应用程序中重用它。我的问题是,每次我调用该方法时,我都想使用数据库中的不同表(因此 GTMD_Financials 更改)
我似乎无法弄清楚,并且非常感谢任何形式的帮助或示例。
int locationControl = 78;
DataClasses1DataContext dc = new DataClasses1DataContext();
dc.GTMD_Financials.ToList().ForEach(x =>
{
KPIEntrys uc = new KPIEntrys(); // UserControl
uc.KPI = x.KPI; // Add data to properties
uc.Status = x.Status.ToString();
uc.Goal = x.Goal.ToString();
uc.Currently = x.Currently.ToString();
bool checkaction = x.ShowAction == true ? uc.ShowAction = true : uc.ShowAction = false;
bool checkstats = x.ShowStats == true ? uc.ShowStats = true : uc.ShowStats = false;
bool checkstatus = x.Status < x.StatusSignal ? uc.StatusGood = true : uc.StatusGood = false;
uc.Location = new Point(21, locationControl);
this.Controls.Add(uc); // Add Control to Form
locationControl = locationControl + 34;
}
);
如果有不清楚的地方,请告诉我。提前感谢您的帮助。
编辑:
在我已经得到的帮助下,我似乎无法让它工作。在我已经得到的回复的帮助下,我能够稍微编辑该方法:
int locationControl = 78;
DataClasses1DataContext dc = new DataClasses1DataContext();
public List<Control> LoadKPIs(Table<GTMD_Financial> dbTable)
{
var controls = new List<Control>();
dbTable.ToList().ForEach(x =>
{
KPIEntrys uc = new KPIEntrys();
uc.KPI = x.KPI;
uc.Status = x.Status.ToString();
uc.Goal = x.Goal.ToString();
uc.Currently = x.Currently.ToString();
uc.ShowAction = (bool)x.ShowAction;
uc.ShowStats = (bool)x.ShowStats;
uc.StatusGood = x.Status < x.StatusSignal;
uc.Location = new Point(21, locationControl);
controls.Add(uc);
locationControl = locationControl + 34;
}
);
return controls;
}
所以让我重新表述我的问题:当我调用方法时如何更改类:LoadKPIs(Table< GTMD_Financial > dbTable?所以 GTMD_Finacial 发生了变化。