如何在ASP.NET中计算网格视图中大于零的总行数

本文关键字:大于 视图 网格 ASP NET 计算 | 更新日期: 2024-09-23 08:26:18

我在使用SQLDataSource的asp.net页面中有网格视图。我希望在标签中显示大于零的总行数。文本(我的列是价格列,我想计算价格大于零的行我该怎么做我尝试使用以下代码获取总行数:int totalRows=e.AffectedRows感谢

如何在ASP.NET中计算网格视图中大于零的总行数

您必须使用linq来比较价格>0 的值

例如:-

var count = objVar.Where(x=>x.price > 0).count();

为了直接从GridView中的单元格中读取,我使用了一个名为GetPrice()的方法,该方法返回特定列中所有价格的列表。

注意:您应该在GridView中指定哪一列与Price相关,因此Price列的索引(Zero-Based)必须在PriceColumnIndex常量变量中设置。

protected void Page_Load(object sender, EventArgs e)
{
    List<double> lstPrice = GetPrice();
    double dblPriceSum = lstPrice.Where(p => p > 0).Sum();
    lblCount.Text = string.Format("{0:#,##0  Rls}", dblPriceSum);
}
private List<double> GetPrice()
{
    const int PriceColumnIndex = 0;
    List<double> resList = new List<double>();
    for (int i = 0; i < GridView1.Rows.Count; i++)
    {
        string strPrice = GridView1.Rows[i].Cells[PriceColumnIndex].Text;
        double dblPrice;
        bool blIsParsable = double.TryParse(strPrice, out dblPrice);
        if (blIsParsable)
            resList.Add(dblPrice);
    }
    return resList;
}

祝你好运。