DataGrid数据绑定
后台数据绑定
用户场景是生成报表,展示公司各员工每个月的绩效
数据结构
包括报表和单个员工绩效两个实体
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数据绑定的更多相关文章
- WPF之 DataGrid数据绑定
DataGrid控件是显示数据的控件,从一个对象集合获取信息并在具有行和单元格的网格中显示信息.每行和一个单独的对象相对应,并且每列和该对象中的一个属性相对应. DataGrid控件添加了许多在WPF ...
- asp.net MVC的EF与easyui DataGrid数据绑定
页面代码 @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewpor ...
- jQuery easyui datagrid数据绑定
1.绑定json数据 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type&qu ...
- miniui MVC datagrid数据绑定
数据绑定 Default.cshtml <div id="datagrid1" class="mini-datagrid" style="wid ...
- easy ui datagrid 数据绑定
1.前台页面 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> ...
- WPF DataGrid 数据绑定、样式、分页、增删改查,连接Access数据库
先上效果图: XAML: <UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&q ...
- 【Jqurey EasyUI+Asp.net】----DataGrid数据绑定,以及增、删、改(SQL)
也懒得打其他字了,直接进入主题吧 1.首先,数据表Rex_Test ID int 自增 tName varchar(10) 姓名 tEmail varchar(80) 邮箱 2.至于代码里的Jqure ...
- WPF DataGrid数据绑定
<DataGrid Name="date_grid" Grid.Column="0" ItemsSource="{Binding Portinf ...
- WPF DataGrid 数据绑定之"List配合Dictionary"
WPF 的DataGrid是WPF中最为强大的控件之一,可以通过各种方式绑定 例如通过最为形似的dataTable来绑定 本文则用List<Dictionary<K,V>>来绑 ...
随机推荐
- How to use ftp in a shell script
转载How to use ftp in a shell script How to use ftp in a shell script Bruce EdigerBruce Ediger's home ...
- Android的NDK开发(3)————JNI数据类型的详解
在Java中有两类数据类型:primitive types,如,int, float, char:另一种为reference types,如,类,实例,数组. 注意:数组,不管是对象数组还是基本类型数 ...
- 十年磨一剑 Delphi再写传奇(不争辩,不解释,十年坚持不懈的努力)
新年伊始,英巴卡迪诺公司(Embarcadero)就在其官网发布了“激动人心的RAD Studio2018年发展规划”公告(见上图).公告中指出,将在于2018年发布10.3.X新版本,新版本兼容Ex ...
- erlang tcp发包速度测试
http://blog.sina.com.cn/s/blog_96b8a1540101317m.html 这段时间我们的项目遇到广播包的一些性能问题,想起之前看到yufeng老大提到的1s广播40K包 ...
- java读取.properties文件乱码
1.config.properties文件写不进中文,写进去都变成了unicode,解决的方法是右键该文件--Properties--Resource--Text file encoding ,选ot ...
- TensorFlow: couldn’t open CUDA library cupti64_80.dll、InternalError: Blas SGEMM launch failed
1. couldn't open CUDA library cupti64_80.dll Win10 TensorFlow(gpu)安装详解 在资源管理器中查询 cupti64_80.dll 的位置. ...
- 关于JDBC连接数据库时出现的Public Key Retrieval is not allowed错误
问题描述 最近在学习MyBatis框架,参考官方的文档通过配置文件的方式已经实现了通过Configuration配置文件和mapper映射文件访问mysql8数据库,于是想试试不使用XML文件去构建S ...
- WPF 好看的矢量图标
原文:WPF 好看的矢量图标 本文告诉大家一个好用的网站,里面提供很多好看的图标. 本文介绍的网站是 Xamalot 里面有很多好看的图标. 例如我找到了一个好看的图标 我只需要点击下面的下载就可以了 ...
- qemu使用copy-on-write(COW)磁盘
写时复制(copy-on-write,缩写COW)技术不会对原始的镜像文件做更改,变化的部分写在另外的镜像文件中,这种特性在qemu中只有QCOW格式支持,多个 COW 文件可以指向同一映像同时测试多 ...
- WinForm - 窗体淡入效果界面的简单实现方法
WinForm窗体淡入效果主要使用到控件的Opacity属性 首先在WinForm窗体中拖入一个Timer控件,然后再Timer控件的Tick事件添加如下代码: private void timer1 ...