Calculation of currency sum

Asked

Viewed 317 times

0

I need a help to make a calculation actually a sum per coin, as shown in the image below:

inserir a descrição da imagem aqui

follows my code.

private void ListaGrid()
    {
        conex.Open();
        string strSQL = @"SELECT DISTINCT
                        SY.YF_DESC_SI                               AS [TIPO MOEDA],
                        RTRIM(SC.C7_PRODUTO) + ' - ' + SC.C7_DESCRI AS PRODUTO,
                   SUM(SC.C7_QUANT)                            AS QTDA, 
                   SUM(SC.C7_TOTAL * SC.C7_TXMOEDA)            AS TOTAL,
                   SUM(SC.C7_TOTAL)                            AS [TOTAL MOEDA]
               FROM  SC7010 AS SC
          INNER JOIN SM2010 AS SM WITH (NOLOCK) ON SM.M2_DATA = SC.C7_EMISSAO
          INNER JOIN SA2010 AS SA WITH (NOLOCK) ON SA.A2_COD = SC.C7_FORNECE
               INNER JOIN SYF010 AS SY WITH (NOLOCK) ON SY.YF_MOEFAT = SC.C7_MOEDA
               WHERE SC.D_E_L_E_T_ <> '*' 
               AND SC.C7_EMISSAO BETWEEN CONVERT(datetime, '" + txtDtInicial.Text + "', 103) AND CONVERT(datetime,'" + txtDtFinal.Text + "', 103) AND SA.A2_COD = '" + txtCodFornec.Text + "' GROUP BY SC.C7_PRODUTO, SC.C7_DESCRI, SY.YF_DESC_SI ORDER BY RTRIM(SC.C7_PRODUTO) + ' - ' + SC.C7_DESCRI";

        comando = new SqlCommand(strSQL, conex);

        try
        {
            SqlDataAdapter dados = new SqlDataAdapter(comando);
            DataTable dtLista = new DataTable();
            dados.Fill(dtLista);

            DGW_PedComprasPic.DataSource = dtLista;
        }
        catch
        {
            MessageBox.Show("Não existem dados a serem encontrados");
        }

        double QTDA = 0;
        foreach (DataGridViewRow rowKG in DGW_PedComprasPic.Rows)
        {
            QTDA += Convert.ToDouble(rowKG.Cells["QTDA"].Value);
        }
        txtProdkg.Text = Convert.ToString(QTDA);
        txtProdkg.Text = QTDA.ToString("N2");

        double TotalReal = 0;
        foreach (DataGridViewRow rowR in DGW_PedComprasPic.Rows)
        {
            TotalReal += Convert.ToDouble(rowR.Cells["TOTAL"].Value);
        }
        txtReal.Text = Convert.ToString(TotalReal);
        txtReal.Text = TotalReal.ToString("C");

        //    double TotalDolar = 0;
        //    foreach (DataGridViewRow rowD in DGW_PedComprasPic.Rows)
        //    {
        //        TotalDolar += Convert.ToDouble(rowD.Cells["TOTAL MOEDA"].Value);
        //    }
        //    txtDolar.Text = Convert.ToString(TotalDolar);
        //    txtDolar.Text = "$ " + TotalDolar.ToString("N2");
        //    double TotalEuro = 0;
        //    foreach (DataGridViewRow rowE in DGW_PedComprasPic.Rows)
        //    {
        //        TotalEuro += Convert.ToDouble(rowE.Cells["TOTAL MOEDA"].Value);
        //    }
        //    txtEuro.Text = Convert.ToString(TotalEuro);
        //    txtEuro.Text = "€ " + TotalEuro.ToString("N2");

        //conex.Close();
    }
  • And what help you need?

  • I need to check the CURRENCY TYPE column in the datagrid, and add. EX. I have 4 lines with EURO currency, I need to add the value that this in the column TOTAL CURRENCY of everything that is EURO and shows the above txtbox with the name Total €€

  • you have 2 paths, do via SQL and return the data already grouped.. using computer by, or sub-queries, or stored procedures or listening to the Gridview1_rowdatabound as soon as the values are filled in the column vc go adding.

  • Yes but I need to add by type of currency, as I do it directly in datagridview

  • if (e.Row.Rowtype == Datacontrolrowtype.Datarow) { var dr = ((Datarowview)e.Row.Dataitem). Row; if dr[0]. Tostring() ="euro"{ //sum

  • I put a basic example for Asp.net and windows.form

  • It hasn’t worked out here yet. I don’t know if I can explain myself directly, I need to check the column COIN TYPE, in this column I have lines with two COIN TYPES "EURO" and "US DOLLAR" now I need to check the value of each row in the column TOTAL CURRENCY, so I need to add the EURO coin, and also add up the value of the US dollar currency, but separately showing in a txtbox the total value of the EURO currency and the total value of the US dollar.

Show 2 more comments

1 answer

1

You have 2 paths.

  • Do through SQL (Stored Procedure, or computer by, etc) and already return the ready data.
  • Or through the Gridview1_rowdatabound (ASP.NET) property if it is Windows.Form dataGridView1_CellFormatting or dataGridView1_RowsAdded

Example of how to manipulate Rowdatabound

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {//tem dados

     var dr = ((DataRowView)e.Row.DataItem).Row;
     if(dr[0].ToString()=="Euro")
     {
        //somaEuro += Convert.ToDecimal(dr[3]);
     }
    }
}

example of dataGridView1_CellFormatting

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {

}

https://stackoverflow.com/questions/25192961/cell-formatting-in-datagridview-on-databindingcomplete


More information:

https://msdn.microsoft.com/pt-br/library/system.web.ui.webcontrols.gridview.rowdatabound(v=vs.110). aspx

Browser other questions tagged

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