我在C#WPF中有一个DataGrid,我希望以有组织的方式列出数据,但问题是动态绑定列加载很难处理,因为我的类需要一个动态字段名称?
代码
public EmployeeDataGrid_BiWeek(string fromData, string untilDate, Benefit[] selectedBenefits, Employee[] selectedEmployees)
{
InitializeComponent();
foreach (Benefit benefit in selectedBenefits)
{
var col = new DataGridTextColumn();
col.Header = benefit.getName();
col.Binding = new Binding(benefit.getName());
// ^^ the above binding is based on the field name
mainDataGrid.Columns.Add(col);
}
foreach (Employee emp in selectedEmployees)
{
mainDataGrid.Items.Add(new BiEmpGridItem(emp));
}
}
public class BiEmpGridItem {
public string Name { get;set; }
public string Birthday { get; set;}
// need a field here with the name of the binding?
public BiEmpGridItem(Employee ee)
{
this.Name = ee.lastName + ", " + ee.firstName;
this.Birthday = ee.getDateOfBirth();
}
}
}
问题
如上所示,似乎我需要在我的班级中使用与用户指定名称相同名称的字段名称。有没有办法可以使用benefit.getName()作为名称来创建一个String字段?如果不是,如何解决绑定基于字段名称的事实?
每个员工都可以获得多重利益
答案 0 :(得分:0)
你能做这样的事吗?
public EmployeeDataGrid_BiWeek(string fromData, string untilDate, Benefit[] selectedBenefits, Employee[] selectedEmployees)
{
InitializeComponent();
string[] benefit_name = new string[selectedBenefits.count()];
int count = 0;
foreach (Benefit benefit in selectedBenefits)
{
var col = new DataGridTextColumn();
col.Header = benefit.getName();
benefit_name[count] = benefit.getName();
col.Binding = new Binding(benefit.getName());
// ^^ the above binding is based on the field name
mainDataGrid.Columns.Add(col);
count++;
}
foreach (Employee emp in selectedEmployees)
{
BigEmpGridItem item = new BiEmpGridItem(emp);
int x = ????; // The number of the benefit for this employee
item.BenefitName = benefit_name[x];
mainDataGrid.Items.Add(item);
}
}
public class BiEmpGridItem {
public string Name { get;set; }
public string Birthday { get; set;}
public string BenefitName {get; set;} // add this
public BiEmpGridItem(Employee ee)
{
this.Name = ee.lastName + ", " + ee.firstName;
this.Birthday = ee.getDateOfBirth();
}
}
}
使用列表记录每位员工的多项福利:
public void EmployeeDataGrid_BiWeek(string fromData, string untilDate, Benefit[] selectedBenefits, Employee[] selectedEmployees)
{
InitializeComponent();
List<string> benefit_names = new List<string>();
int count = 0;
// Get this employees selected benefits
foreach (Benefit benefit in selectedBenefits)
{
var col = new DataGridTextColumn();
col.Header = benefit.getName();
benefit_names.Add(benefit.getName());
col.Binding = new Binding(benefit.getName());
// ^^ the above binding is based on the field name
mainDataGrid.Columns.Add(col);
count++;
}
// Create the grid
foreach (Employee emp in selectedEmployees)
{
BiEmpGridItem item = new BiEmpGridItem(emp);
item.BenefitName = benefit_names;
mainDataGrid.Items.Add(item);
}
}
public class BiEmpGridItem
{
public string Name { get;set; }
public string Birthday { get; set;}
public List<string> BenefitName {get; set;} // add this
public BiEmpGridItem(Employee ee)
{
this.Name = ee.lastName + ", " + ee.firstName;
this.Birthday = ee.getDateOfBirth();
}
}
答案 1 :(得分:0)
所以我必须找到一种动态绑定数据或至少逐行插入数据的方法....
结束退出对象绑定的想法并使用了dataTable,这里是其他人的代码。
List<string> dates = new List<string>();
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("BirthDate");
foreach (String s in company.payDays)
{
DateTime compd = DateTime.Parse(s);
if (compd > startDate && compd < endDate)
{
dates.Add(s);
dt.Columns.Add(s);
}
}
foreach (Employee emp in selectedEmployees)
{
var row = dt.NewRow();
row["Name"] = emp.getLastName() + ", " + emp.getFirstName();
row["BirthDate"] = emp.getDateOfBirth();
dt.Rows.Add(row);
}
mainDataGrid.AutoGenerateColumns = true;
mainDataGrid.DataContext = dt.DefaultView;
<DataGrid x:Name="mainDataGrid" DockPanel.Dock="Bottom" ItemsSource="{Binding}" IsReadOnly="True" ClipboardCopyMode="IncludeHeader">