如果使用中继器,如何计算总和

时间:2018-01-12 09:25:19

标签: c# html

        SqlConnection con = new SqlConnection(connectionString: ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        con.Open();
        String cquery = "SELECT cart.ProductID, ProName, Size, Colour, Price  FROM cart, Products WHERE Custid=" + Session[custid] + "AND Products.ProductID = cart.ProductID";
        SqlCommand ccmd = new SqlCommand(cquery, con);
        DataTable dataTable = new DataTable();

        CRepeater.DataSource = ccmd.ExecuteReader();
        CRepeater.DataBind();
        con.Close();
        DataRow[] dr = dataTable.Select("SUM(Price)");
        Label3.Text = Convert.ToString(dr[0]); ;

我正在使用转发器来显示数据库中的数据,我似乎无法弄清楚如何使用转发器获取总和

1 个答案:

答案 0 :(得分:3)

计算DataTable中列总和的最佳方法是使用DataTable.Compute方法。

// Declare an object variable.
object sumObject;
sumObject = table.Compute("Sum(Price)", "");

在总金额标签中显示结果,如下所示:

Label3.Text = sumObject.ToString();

<强>更新

你缺少加载DataTable,试试这个

 var dataReader = ccmd.ExecuteReader();
 var dataTable = new DataTable();
 dataTable.Load(dataReader);
 CRepeater.DataSource = dataTable;
 CRepeater.DataBind();