后台数据绑定

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

数据结构

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

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. 多事务运行并发问题spring学习笔记——数据库事务并发与锁详解

    多事务运行并发问题 在实际应用中,往往是一台(或多台)服务器向无数客户程序提供服务,当服务器查询数据库获取数据时,如果没有采用必要的隔离机制,可能会存在数据库事务的并发问题,下面是一些常见的并发问题分 ...

  2. Perl自动释放Licence启动Verdi

    Perl自动释放Licence启动Verdi 在工作中,遇到verdi的License不够的情况,某些人占用了多个License,为及时获得一个可用的License,写了一个perl来kill运行时间 ...

  3. fortran 函数的调用标准

    Fortran函数的调用标准在编译时使用iface声明.如iface:default.表示採用的是default标准. fortran的调用标准有 [1] default: Tells the com ...

  4. 学习Numpy基础操作

    # coding:utf-8 import numpy as np from numpy.linalg import * def day1(): ''' ndarray :return: ''' ls ...

  5. 手动打war包进行部署测试

    有的时候项目跑不起来但是又不知道tomcat问题还是其他问题,往往新建个项目,打成war进行部署.今天找到个好方法,手动打成war,然后进行部署测试. image.png image.png 创建一个 ...

  6. Swift开发教程--关于Existing instance variable &#39;_delegate&#39;...的解决的方法

    xcode编译提示问题:Existing instance variable '_delegate' for property 'delegate' with  assign attribute mu ...

  7. 小强的HTML5移动开发之路(48)——(小练习)新闻订阅系统【1】

    一.总体设计 二.数据库设计 --新闻类别表 create table news_cate( news_cateid int primary key auto_increment, news_icon ...

  8. Android的NDK开发(5)————Android JNI层实现文件的read、write与seek操作

    1. 在Android的Java层实现文件的读写操作是非常简单的,可以参看之前写的博文:http://blog.csdn.net/conowen/article/details/7296121 在JN ...

  9. Android与IOS的UUID的区别

    UUID含义是通用唯一识别码 (Universally Unique Identifier),这 是一个软件建构的标准,也是被开源软件基金会 (Open Software Foundation, OS ...

  10. 【hdu 3032】Nim or not Nim?

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...