【发布时间】:2019-08-02 02:25:04
【问题描述】:
DataGrid 列绑定到具有从数据库填充的十进制数据类型的 DataTable 列。
<DataGridTextColumn Binding="{Binding Pay, UpdateSourceTrigger=PropertyChanged,Mode=TwoWay, Header="Sum"/>
更改每个单元格后,我需要计算列中的总和。
private void dgPays_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) {
if (dtPay == null || dtPay.Rows.Count == 0) {
return;
}
decimal Pay;
DataRow[] rr = dtPay.Select("IsTotal=1");
foreach (DataRow r in rr) {
dtPay.Rows.Remove(r);
}
Decimal.TryParse(dtPay.Compute("SUM(Pay)", "").ToString(), out Pay);
dtPay.Rows.Add(null, null, null, null, null, null, Pay, null, true);
}
但是,由于此绑定,当用户输入带有前导零“-0.xxxx”的负小数时,输入时减号会丢失,“-0”会转换为“0”。此类数字只能输入“-.xxxx”,自动转换为“-0.xxxx”,不方便用户使用。
DataTable 是从数据库中填充的,然后将值从它保存到数据库中,我无法更改“Mode=TwoWay”绑定。 如果我在单元格编辑结束后更改“UpdateSourceTrigger=PropertyChanged”不起作用“dtPay.Compute("SUM(Pay)", "")"。
如何输入“-0”?
【问题讨论】: