datagrid中添加合计行计算合计

    在公司demo项目实习中,我有一个功能是展示所有支出明细后,在下面添加一行合计,计算所有支出明细之和。
    首先我想到的是用easyui里datagrid有一个属性showFooter,就是添加尾行,无论在页面添加多少条数据,合计行都会位于所有记录的最下方,而且比较美观。于是我在我的代码中添加了showFooter:true的属性,如下:
<div id="payGrid" wj-type="datagrid"
	 wj-options="{title:'支出明细',listUrl:'<ww:actionUri action="dirPayDetailData"/>',idField:'uuid',columns:[[
	 	{ field: 'ck', checkbox: true },
	 { title: 'uuid', field: 'uuid', width: 100,hidden:true},	
	{ title: '员工名称', field: 'empName', width: 100},
	{ title: '支出金额', field: 'payMoney', width: 150 },
	{ title: '支出时间', field: 'payDate', width: 150,formatter:function(value,row,index){
	    return myFunction(row.payDate);
	}},
	{ title: '创建时间', field: 'createTime', width: 300},
	{ title: '修改时间', field: 'editTime', width: 300},
	{ title: '创建者', field: 'creater', width: 300}
	]]<span style="color:#ff0000;">,showFooter:true,footer:[{'empName':'合计','payMoney':600.0}]</span>}">

这是模仿easyui官网的demo写的。官网demo的数据来源和我的不太相似,如果有兴趣,大家可以了解一下官网的。easyui官网
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WPF DataGrid可以通过设置属性来实现自动添加。具体步骤如下: 1. 在DataGrid控件添加一个新: <DataGrid ItemsSource="{Binding MyData}" AutoGenerateColumns="False" CanUserAddRows="True"> <DataGrid.Columns> <DataGridTextColumn Header="Column 1" Binding="{Binding Column1}" /> <DataGridTextColumn Header="Column 2" Binding="{Binding Column2}" /> </DataGrid.Columns> </DataGrid> 2. 在ViewModel初始化数据源: public class ViewModel { public ObservableCollection<MyDataModel> MyData { get; set; } public ViewModel() { MyData = new ObservableCollection<MyDataModel>(); //添加默认 MyData.Add(new MyDataModel()); } } 3. 在MyDataModel设置自动添加: public class MyDataModel : INotifyPropertyChanged { //实现INotifyPropertyChanged接口 public event PropertyChangedEventHandler PropertyChanged; //两列数据 private string _column1; public string Column1 { get { return _column1; } set { _column1 = value; RaisePropertyChanged("Column1"); //添加 if (string.IsNullOrEmpty(value) && string.IsNullOrEmpty(Column2)) { RaiseRequestNewRow(); } } } private string _column2; public string Column2 { get { return _column2; } set { _column2 = value; RaisePropertyChanged("Column2"); //添加 if (string.IsNullOrEmpty(value) && string.IsNullOrEmpty(Column1)) { RaiseRequestNewRow(); } } } //请求添加事件 public event EventHandler RequestNewRow; private void RaiseRequestNewRow() { EventHandler handler = RequestNewRow; if (handler != null) { handler(this, EventArgs.Empty); } } //通知数据变更 private void RaisePropertyChanged(string propertyName) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(propertyName)); } } } 4. 在MainWindow订阅RequestNewRow事件: public MainWindow() { InitializeComponent(); var viewModel = new ViewModel(); this.DataContext = viewModel; viewModel.MyData.CollectionChanged += MyData_CollectionChanged; } private void MyData_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add) { //添加 if (e.NewItems != null && e.NewItems.Count > 0) { var newItem = e.NewItems[0] as MyDataModel; newItem.RequestNewRow += NewItem_RequestNewRow; } } } private void NewItem_RequestNewRow(object sender, EventArgs e) { var viewModel = this.DataContext as ViewModel; if (viewModel != null) { viewModel.MyData.Add(new MyDataModel()); } } 这样设置后,当用户在最后一输入完成后,DataGrid会自动添加一个新
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值