在 GridView asp.net 中显示具有特定值的行数



我使用以下代码在网格视图中显示总行数

Label1.Text = "Total Number of Rows: " + e.AffectedRows.ToString();

现在我有一列 [NewColumn] 有两个值,"是"和"Null",我怎么知道值为"YES"的行数,并显示为"是行数:[具有 YES 的行]/[总行]"?

您可以使用 RowDataBound 事件来实现此目的。在那里检查列NewColumn的正确值并增加总计。

int totalRowsWithYes = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//cast the row back to a datarowview
DataRowView row = e.Row.DataItem as DataRowView;
//check the column value and increment
if (row["NewColumn"].ToString() == "YES")
{
totalRowsWithYes++;
}
}
}

最新更新