后台数据绑定

用户场景是生成报表,展示公司各员工每个月的绩效

数据结构

包括报表和单个员工绩效两个实体

public class Report
{
/// <summary>
/// 统计时间
/// </summary>
public string StatisticalDate { get; set; }
public List<ReportDetail> ReportDetails { get; set; }
}
public class ReportDetail
{
/// <summary>
/// 职员姓名
/// </summary>
public string EmployeeName { get; set; }
/// <summary>
/// 统计数据
/// </summary>
public decimal Data { get; set; }
}

关键代码

DataGrid dataGrid = new DataGrid();
var _ds = new DataSet("Test");
Dt = _ds.Tables.Add("月度绩效表");
//create columns
//创建列
Dt.Columns.Add("月份");
foreach (var item in reports[0].ReportDetails)
{
Dt.Columns.Add(item.EmployeeName);
}
//fill data to rows
//赋值数据
for(int i=0;i< reports.Count;i++)
{
var theRow = Dt.NewRow();
theRow[0] = reports[i].StatisticalDate;
for (int j = 0; j < reports[i].ReportDetails.Count; j++)
{
theRow[j+1] = reports[i].ReportDetails[j].Data;
}
Dt.Rows.Add(theRow);
}
//数据绑定
dataGrid.ItemsSource = Dt.AsDataView();
//将控件添加到Grid
MyGrid.Children.Add(dataGrid);

示例代码

https://github.com/zLulus/NotePractice/blob/dev3/WPF/WpfDemo/Bind/DataGridBackgroundBind.xaml

https://github.com/zLulus/NotePractice/blob/dev3/WPF/WpfDemo/Bind/DataGridBackgroundBind.xaml.cs

其他:列头重复解决方案

当前用户场景,如果遇到行列互换,即将员工姓名和月份互换,可能出现列名相同的问题(员工同名),则最好将列头绑定改为员工姓名+员工编号,保证唯一性,前端只显示名称,绑定"名称+ID"

前端数据绑定

数据结构

包括教师和教师信息扩展两个实体

public class Teacher
{
public string SchoolNumber { get; set; }
public string Name { get; set; }
public string Sex { get; set; }
public TeacherDetailInfo TeacherDetailInfo { get; set; }
}
public class TeacherDetailInfo
{
public DateTime EntryTime { get; set; }
public string Address { get; set; }
}

关键代码

<DataGrid ItemsSource="{Binding }" AutoGenerateColumns="False" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="编号" Binding="{Binding SchoolNumber}"/>
<DataGridTextColumn Header="姓名" Binding="{Binding Name}"/>
<DataGridTextColumn Header="性别" Binding="{Binding Sex}"/>
<!--格式化日期-->
<DataGridTextColumn Header="入职时间" Binding="{Binding Path=TeacherDetailInfo.EntryTime, StringFormat=\{0:yyyy年MM月dd日\}}"/>
<!--如果这里是双向绑定,则是下面的写法,Mode是双向(TwoWay),触发器是变化即触发-->
<!--<DataGridTextColumn Header="入职时间" Binding="{Binding Path=TeacherDetailInfo.EntryTime,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>-->
<DataGridTextColumn Header="住址" Binding="{Binding Path=TeacherDetailInfo.Address}"/>
</DataGrid.Columns>
</DataGrid>

示例代码

https://github.com/zLulus/NotePractice/blob/dev3/WPF/WpfDemo/Bind/DataGridBindMultiData.xaml

https://github.com/zLulus/NotePractice/blob/dev3/WPF/WpfDemo/Bind/DataGridBindMultiData.xaml.cs

最新文章

  1. MySQL学习笔记十三:表分区
  2. C 语言学习 第五次作业总结
  3. [MAVEN]二、常用命令
  4. acdream1421 TV Show (枚举)
  5. zabbix3.2.0beta2 监控模版
  6. Asp.net设计模式笔记之三:业务逻辑层的组织
  7. HDU 1010 Tempter of the Bone --- DFS
  8. directx11编程中遇到的错误及解决方法
  9. ★不容错过的PPT教程!
  10. 云计算一:VMware workstation的安装和使用教程
  11. 在Ubuntu 12.04下创建eclipse的桌面链接
  12. JDK1.8源码分析之Comparable &amp;&amp; Comparator
  13. RESTful Java client with Apache HttpClient / URL /Jersey client
  14. node csv
  15. linux下常用命令:
  16. (转)找回vss超级管理员密码
  17. vue+node+mongoDB 火车票H5(六)---城市列表保存到MongoDB数据库并且启用node.js服务
  18. Python开发基础-Day23try异常处理、socket套接字基础1
  19. CentOS 7 安装 docker-compose
  20. linux开机过程

热门文章

  1. ios开发瀑布流框架的封装
  2. mysql zip文件安装
  3. 【u015】兽径管理
  4. zzuli OJ 1128: 课程平均分
  5. 避免if语句的深层次嵌套
  6. Birt
  7. VS关于 _CRT_SECURE_NO_WARNINGS 警告说明
  8. TensorFlow中卷积
  9. 集群搭建Solr
  10. 面试无忧之Zookeeper总结心得