How to take a column in the database, perform a calculation and send it to the grid

Asked

Viewed 299 times

4

I want to perform a calculation using the value of the VAL_VIDA_UTIL field of my BD and then save the result since calculation in a new variable VAL_DEPRECIACAO and pass to my fieldName, but I’m not getting it. Someone could help me?

My code Behind:

public partial class DepreciacaoAtivos : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DateTime INICIO_ANO_AGRICOLA = new DateTime(DateTime.Now.Year);
            SqlDepreciacaoAtivos.SelectParameters.Add("INICIO_ANO_AGRICOLA", DbType.Date,
                INICIO_ANO_AGRICOLA.ToShortDateString());

            if (Session["DepreAtivos"] != null)
            {
                Session.Remove("DepreAtivos");
            }

            if (Session["COD_PROPRIEDADE_ATUAL"] == null)
            {
                return;
            }

            DataTable dt = GetTable();

            DataView dvDepreAtivos = (DataView)SqlDepreciacaoAtivos.Select(DataSourceSelectArguments.Empty);
            DataTable dtDepreAtivos = dvDepreAtivos.ToTable();

            foreach (DataRow dr in dtDepreAtivos.Rows)
            {
                int VAL_DEPRECIACAO;

                if ((Int32)dr["VAL_VIDA_UTIL"] == 0)
                {
                    VAL_DEPRECIACAO = 10;
                }
            }

            Session["DepreAtivos"] = dt;

            ASPxPivotGrid1.DataBind();
        }
    }

    private DataTable GetTable()
    {
        DataTable dt = (DataTable)Session["DepreAtivos"];

        if (dt == null)
        {
            dt = new DataTable();
            dt.Columns.Add("DAT_ENTRADA", typeof(DateTime));
            dt.Columns.Add("DAT_SAIDA", typeof(DateTime));
            dt.Columns.Add("DES_ATIVO", typeof(string));
            dt.Columns.Add("VAL_ATIVO", typeof(Decimal));
            dt.Columns.Add("VAL_VIDA_UTIL", typeof(Int32));
            dt.Columns.Add("TIP_ATIVO", typeof(Int32));
            dt.Columns.Add("TXT_OBSERVACAO", typeof(String));
            dt.Columns.Add("DAT_ALTERADO", typeof(DateTime));
            dt.Columns.Add("COD_PROPRIEDADE", typeof(UniqueConstraint));
            dt.Columns.Add("IMG_FOTO", typeof(Image));

            dt.PrimaryKey = new DataColumn[] { dt.Columns["COD_ATIVO"] };

            Session["DepreAtivos"] = dt;
        }

        return dt;
    }
}

My page:

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
      <table class="BottomMargin">
    <tr>

        <td>
            <dx:ASPxButton ID="ASPxButtonImprimir" runat="server" Text="Imprimir"></dx:ASPxButton>
        </td>
        <td>
            <dx:ASPxButton ID="ASPxButtonExportar" runat="server" Text="Exportar"></dx:ASPxButton>
        </td>
          <td>
             &nbsp;&nbsp
        </td>
        <td>Ano Agrícola:
        </td>
         <td>
             &nbsp
        </td>
        <td>
            <dx:ASPxComboBox ID="ddlField" runat="server" AutoPostBack="true" Width="120" 
                SelectedIndex="0">
                <Items>
                    <dx:ListEditItem Text="2014/2015" Value="0" />
                </Items>
            </dx:ASPxComboBox>
        </td>
         <td>
             &nbsp;&nbsp
        </td>
        <td>
            <dx:ASPxButton ID="ASPxButtonAtualizar" runat="server" Text="Atualizar"></dx:ASPxButton>
        </td>

    </tr>
</table>
<br />

    <Fields>
         <dx:PivotGridField Area="FilterArea" AreaIndex="0" FieldName="DAT_SAIDA" ID="fielDataSaida"
            Caption="Data de Saída" GroupInterval="Date" UnboundFieldName="fielDAT_TRANSACAO"/>

        <dx:PivotGridField Area="FilterArea" AreaIndex="1" FieldName="" ID="fielDataExaustao"
            Caption="Data de Exaustão" GroupInterval="Date" UnboundFieldName="fielDataExaustao"/>

         <dx:PivotGridField Area="DataArea" AreaIndex="0" FieldName="VAL_ATIVO" ID="fielValorRs"
            Caption="Valor (R$)" />

         <dx:PivotGridField Area="DataArea" AreaIndex="1" FieldName="VAL_DOLAR" ID="fielValorUs"
            Caption="Valor (US$)" />

        <dx:PivotGridField Area="DataArea" AreaIndex="2" FieldName="VAL_VIDA_UTIL" ID="fielVidaUtil"
            Caption="Vida Útil" />

        <dx:PivotGridField Area="DataArea" AreaIndex="3" FieldName="" ID="fielVAL_DEPRECIACAO"
            Caption="Depreciação (R$)" />

        <dx:PivotGridField Area="DataArea" AreaIndex="4" FieldName="GRUPO_CONTA" ID="fielDepreciacaoUs"
            Caption="Depreciação (US$)" />

         <dx:PivotGridField Area="RowArea" AreaIndex="0" FieldName="DES_ATIVO" ID="fielTipoAtivo"
            Caption="Tipo de Ativo" />

        <dx:PivotGridField Area="RowArea" AreaIndex="1" FieldName="TXT_OBSERVACAO" ID="fielDescricao"
            Caption="Descrição" />

        <dx:PivotGridField Area="RowArea" AreaIndex="2" FieldName="DAT_ENTRADA" ID="fielDataEntrada"
            Caption="Data de Entrada" GroupInterval="Date" UnboundFieldName="fielDataEntrada"/>
    </Fields>


</dx:aspxpivotgrid>

<dx:ASPxGridViewExporter ID="ASPxGridViewExporter1" runat="server"></dx:ASPxGridViewExporter>
  <asp:SqlDataSource ID="SqlDepreciacaoAtivos" runat="server" ConnectionString="<%$ ConnectionStrings:AGROSOL_REDEConnection %>" 
      SelectCommand="SELECT ATIVO.COD_ATIVO,
                           ATIVO.DAT_ENTRADA,
                           ATIVO.DAT_SAIDA,
                           ATIVO.DES_ATIVO,
                           ATIVO.VAL_ATIVO,
                           ATIVO.TXT_OBSERVACAO,
                           ATIVO.VAL_VIDA_UTIL,
                           ATIVO.TIP_ATIVO,
                           DOLAR.VAL_DOLAR
                    FROM ATIVO, DOLAR
                    WHERE (ATIVO.COD_PROPRIEDADE = @COD_PROPRIEDADE)
                          AND
                          ((ATIVO.DAT_SAIDA IS NULL) OR (ATIVO.DAT_SAIDA >= @INICIO_ANO_AGRICOLA))
                    ORDER BY ATIVO.TIP_ATIVO">
    <SelectParameters>
        <asp:SessionParameter Name="COD_PROPRIEDADE"    DbType="Guid"       SessionField="COD_PROPRIEDADE_ATUAL"/>
    </SelectParameters>
  </asp:SqlDataSource>

1 answer

0

Hello, there in the if where you assign the value 10, does so:

if ((Int32)dr["VAL_VIDA_UTIL"] == 0)
{
   dr["VAL_DEPRECIACAO"] = 10;
}

And you would have to create in Datatable to use on each line:

private DataTable GetTable()
{
    DataTable dt = (DataTable)Session["DepreAtivos"];

    if (dt == null)
    {
        dt = new DataTable();
        dt.Columns.Add("DAT_ENTRADA", typeof(DateTime));
        dt.Columns.Add("DAT_SAIDA", typeof(DateTime));
        dt.Columns.Add("DES_ATIVO", typeof(string));
        dt.Columns.Add("VAL_ATIVO", typeof(Decimal));
        dt.Columns.Add("VAL_VIDA_UTIL", typeof(Int32));
        dt.Columns.Add("VAL_DEPRECIACAO", typeof(Int32));
        dt.Columns.Add("TIP_ATIVO", typeof(Int32));
        dt.Columns.Add("TXT_OBSERVACAO", typeof(String));
        dt.Columns.Add("DAT_ALTERADO", typeof(DateTime));
        dt.Columns.Add("COD_PROPRIEDADE", typeof(UniqueConstraint));
        dt.Columns.Add("IMG_FOTO", typeof(Image));

        dt.PrimaryKey = new DataColumn[] { dt.Columns["COD_ATIVO"] };

        Session["DepreAtivos"] = dt;
    }

    return dt;
}

In aspx change the display line to:

<dx:PivotGridField Area="DataArea" AreaIndex="3" FieldName="VAL_DEPRECIACAO" ID="fielVAL_DEPRECIACAO" Caption="Depreciação (R$)" />

Browser other questions tagged

You are not signed in. Login or sign up in order to post.