how I can show the sum of in a datagridview column in asp.net

后端 未结 2 1650
无人及你
无人及你 2021-01-23 10:29

i have a datagridview which are used to display a transaction record. And one of the columns in datagridview is \"amount\"

girdview codeenter code here

2条回答
  •  野的像风
    2021-01-23 11:11

    use table.Compute function

       private void ComputeBySalesSalesID(DataSet dataSet)
    {
        // Presumes a DataTable named "Orders" that has a column named "Total."
        DataTable table;
        table = dataSet.Tables["Orders"];
    
        // Declare an object variable. 
        object sumObject;
        sumObject = table.Compute("Sum(Total)", "EmpID = 5");
    
        //Find label
         GridViewRow footer = dgOpenBal.FooterRow;
         var lblTotal = (Label)footer.FindControl("lblTotal");
         lblTotal.Text = sumObject.ToString();
    }
    

    Another approach use stored procedure to get calculated amount from Sproc

    private void ComputeBySalesSalesID(DataSet ds)
    {
             if (ds.Tables[0].Rows.Count > 0)
                    {
                        DataRow drSum = ds.Tables[0].Rows[0];
                        GridViewRow footer = dgOpenBal.FooterRow;
                        var lblTotal = (Label)footer.FindControl("lblTotal");
                        lblTotal.Text = drSum["sum"].ToString();
                    }
    }
    

提交回复
热议问题