在DataGrid单元格中输入以零开头的负数

时间:2019-03-11 17:41:47

标签: wpf datagrid cell

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”?

1 个答案:

答案 0 :(得分:0)

问题解决了 最初更改了单元格的格式

Last Modified time

并添加了Converter

    ul.menu{
        margin: 0px;
        padding: 0px;
        list-style: none;
        margin-left:65%;
    }
    ul.menu li{
        float: left;
        width: 200px;
        height: 40px; 
        background-color:blue;
        opacity: .8;
        line-height: 40px;
        text-align: center;
        font-size: 20px;

    }
    ul li a{
        text-decoration: none;
        color: white;
        display: block;
    }
    ul li a:hover{
        background-color: green;
    }
    ul li ul li{
        display: none;
    }
    ul li:hover ul li{
        display: block;
    }
    li {
        position: relative;
    }
    li ul {
        position: absolute;
        left: 0;
        margin: 0;
        padding: 0;
        display: block;
        width: inherit;
    }