后台数据绑定

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

数据结构

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

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

DataGrid数据绑定的更多相关文章

  1. WPF之 DataGrid数据绑定

    DataGrid控件是显示数据的控件,从一个对象集合获取信息并在具有行和单元格的网格中显示信息.每行和一个单独的对象相对应,并且每列和该对象中的一个属性相对应. DataGrid控件添加了许多在WPF ...

  2. asp.net MVC的EF与easyui DataGrid数据绑定

    页面代码 @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewpor ...

  3. jQuery easyui datagrid数据绑定

    1.绑定json数据 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type&qu ...

  4. miniui MVC datagrid数据绑定

    数据绑定 Default.cshtml <div id="datagrid1" class="mini-datagrid" style="wid ...

  5. easy ui datagrid 数据绑定

    1.前台页面 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> ...

  6. WPF DataGrid 数据绑定、样式、分页、增删改查,连接Access数据库

    先上效果图: XAML: <UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&q ...

  7. 【Jqurey EasyUI+Asp.net】----DataGrid数据绑定,以及增、删、改(SQL)

    也懒得打其他字了,直接进入主题吧 1.首先,数据表Rex_Test ID int 自增 tName varchar(10) 姓名 tEmail varchar(80) 邮箱 2.至于代码里的Jqure ...

  8. WPF DataGrid数据绑定

    <DataGrid Name="date_grid" Grid.Column="0" ItemsSource="{Binding Portinf ...

  9. WPF DataGrid 数据绑定之"List配合Dictionary"

    WPF 的DataGrid是WPF中最为强大的控件之一,可以通过各种方式绑定 例如通过最为形似的dataTable来绑定 本文则用List<Dictionary<K,V>>来绑 ...

随机推荐

  1. js判断是否微信客户端

    上周接到个需求,需求是这样的:用户扫一扫二维码会产生一个链接,该链接会向后端发送个请求,返回一个 apk 的下载地址,用户点击下载按钮可以下载此 apk.然后就发生了问题,经过测试,发现用微信扫一扫打 ...

  2. win7

    http://www.xitongiso.com/?pot http://www.potplayer.org/

  3. windows 空闲超时 非管理员如何破解

    windows 空闲超时 非管理员如何破解

  4. [TypeScript] Create random integers in a given range

    Learn how to create random integers using JavaScript / TypeScript. /** * Returns a random int betwee ...

  5. RSA Android加密的数据服务器上无法解密?

    一.android加密的数据服务器上无法解密? "算法/模式/填充" android的rsa加密方式是--------RSA/ECB/NoPadding或者RSA/None/NoP ...

  6. IT忍者神龟之Hibernat持久化对象-数据表映射配置回想

    1.持久化对象POJO编写规则: 1) 有空參public构造器: 2) 提供标识属性.映射数据表主键: 3) 属性提供setter和getter方法. 4) 属性使用基本数据类型的包装类型.基本类型 ...

  7. 【C/C++学院】(23)Mysql数据库编程--C语言编程实现mysqlclient

    [送给在路上的程序猿] 对于一个开发人员而言,能够胜任系统中随意一个模块的开发是其核心价值的体现. 对于一个架构师而言,掌握各种语言的优势并能够利运用到系统中,由此简化系统的开发,是其架构生涯的第一步 ...

  8. 【Windows Defender Antivirus Service 永久禁用 】

    cmd 管理员运行 执行 reg add “HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows Defender” /v “DisableAn ...

  9. Opencv Surf算子中keyPoints,描述子Mat矩阵,配对向量DMatch里都包含了哪些好玩的东东?

    Surf算法是一把牛刀,我们可以很轻易的从网上或各种Opencv教程里找到Surf的用例,把例程中的代码或贴或敲过来,满心期待的按下F5,当屏幕终于被满屏花花绿绿的小圆点或者N多道连接线条霸占时,内心 ...

  10. 实现上拉加载更多的SwipeRefreshLayout

    转载请标明出处: http://blog.csdn.net/developer_jiangqq/article/details/49992269 本文出自:[江清清的博客] (一).前言: [好消息] ...