在数据网格中的两个单元格之间执行计算

时间:2015-04-01 17:53:24

标签: c# wpf

我有一个WPF数据网格,我想在QTY On HAND上执行计算 如果QtyOnHand< SetMin(Text)或If Qty> SetMax(Text)并将结果显示在Status列中。我该怎么做呢到目前为止我找到的每个教程都是针对WinForms的,并且不能很好地使用WPF数据网格。

我是这里的新手所以如果有人愿意指出我正确的方向,我不反对弄清楚这一点。 enter image description here

我正在从数据集填充网格

   void mainDataSet()
    {
        SqlConnection con = new SqlConnection(str);
        SqlDataAdapter dataAdapter = new SqlDataAdapter(select, con);
        con.Open();
        SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
        con.Close();
        DataSet ds = new DataSet();
        dataAdapter.Fill(ds);
        dataGridView1.IsReadOnly = true;
        dataGridView1.DataContext = ds.Tables[0];

    }

1 个答案:

答案 0 :(得分:1)

为什么不添加具有计算的属性并将列绑定到该属性? 我在你的笔记中假设(文本)那些实际上是字符串值而不是小数...所以转换它们(它们可能是空的吗?)并比较并返回你喜欢的任何字符串状态。请注意,下面的示例假定setmin和setmax永远不会为null或转换失败,如果qtyonhand在边界内,则不返回任何状态(空白)。

public string Status
    {
        get
        {
            decimal _min = Convert.ToDecimal(this.SetMin);
            decimal _max = Convert.ToDecimal(this.SetMax);
            string _status = string.Empty;
            if (this.QtyOnHand < _min)
                _status = "whatever the status is that means not enough on hand.";
            if(this.QtyOnHand > _max)
                _status = "whatever the status is that means too much is on hand.";

            return _status;
        }
    }
相关问题