如何在Gridview Winforms Devexpress中清除有界列的单元格值?

时间:2013-12-07 08:25:06

标签: c# winforms gridview devexpress

如何清除Gridview中有界列的单元格值?我执行一些计算并将int值设置为单元格(有界列)。我再次重新计算并尝试在有限列时间内设置相同的单元格中的值不变,所以我尝试在设置值之前清除单元格值。

 private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
    {
        // % to amt

        object obj = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["DiscountPercentage"]) == DBNull.Value
                            ? 0
                            : (gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["DiscountPercentage"]));
        decimal a = Convert.ToDecimal(obj);
        if (a > 0)
        {
            gridColumn1.UnboundType = DevExpress.Data.UnboundColumnType.Decimal;
            gridColumn1.UnboundExpression = "[UnitPrice] * ([DiscountPercentage] / 100.0)";
        }





        // amt to %
        object dm = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["DiscountAmount"]) == DBNull.Value
                            ? 0
                            : (gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["DiscountAmount"]));
        decimal a1 = Convert.ToDecimal(dm);
        if (a1 > 0)
        {
            gridColumn2.UnboundType = DevExpress.Data.UnboundColumnType.Decimal;
            gridColumn2.UnboundExpression = "Round(([DiscountAmount] / [UnitPrice]) * 100.0, 2)";
        }
    }




    private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
    {
        // % to amt
        object obj = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["DiscountPercentage"]) == DBNull.Value
                            ? 0
                            : (gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["DiscountPercentage"]));
        decimal a = Convert.ToDecimal(obj);

        if (a > 0)
        {


            object obj2 = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["gridColumn1"]);
            int aa = Convert.ToInt32(obj2);
            gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["DiscountAmount"], aa);
        }


        // amt to %
        object dm = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["DiscountAmount"]) == DBNull.Value
                            ? 0
                            : (gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["DiscountAmount"]));
        decimal a1 = Convert.ToDecimal(dm);
        if (a1 > 0)
        {
            object obj3 = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["gridColumn2"]);
            int aa2 = Convert.ToInt32(obj3);
            gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["DiscountPercentage"], aa2);
        }

    }

提前致谢。

1 个答案:

答案 0 :(得分:0)

终于找到了解决这个问题的解决方案。

private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
    {
        var row = gridView1.GetFocusedDataRow();

        object obj1 = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["DiscountPercentage"]) == DBNull.Value
                        ? 0
                        : (gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["DiscountPercentage"]));
        int inte1 = Convert.ToInt32(obj1);

        object obj2 = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["DiscountAmount"]) == DBNull.Value
                        ? 0
                        : (gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["DiscountAmount"]));
        int inte2 = Convert.ToInt32(obj2);

        // Calculating the dicsount %
        if (e.Column == colDiscountAmount)
        {
            if (inte2 == 0)
            {
                return;
            }
            else
            {
                var productPrice = Convert.ToDecimal(row["UnitPrice"]);
                var quan = Convert.ToDecimal(row["Quantity"]);
                var discountAmout = Convert.ToDecimal(row["DiscountAmount"]);
                var temp = (discountAmout * 100) / (productPrice * quan);
                row["DiscountPercentage"] = temp.ToString("n2");
            }
        }

        // Calculating the discount amount
        if (e.Column == colDiscountPercentage)
        {
            if (inte1 == 0)
            {
                return;
            }
            else
            {
                var productPrice = Convert.ToDecimal(row["UnitPrice"]);
                var quan = Convert.ToDecimal(row["Quantity"]);
                var discountPercent = Convert.ToDecimal(row["DiscountPercentage"]);
                var temp1 = (productPrice * quan) * (discountPercent / 100);
                row["DiscountAmount"] = temp1.ToString("n2");
            }
        }
    }