【问题标题】:display number of rows with certain value in gridview asp.net在gridview asp.net中显示具有特定值的行数
【发布时间】:2019-02-11 05:02:26
【问题描述】:
我用下面的代码在gridview中显示总行数
Label1.Text = "Total Number of Rows: " + e.AffectedRows.ToString();
现在我有一列 [NewColumn] 有两个值,“Yes”和“Null”,我怎么知道值为“YES”的行数,并显示为“YES 行数:[rows是]/[总行数]"?
【问题讨论】:
标签:
asp.net
sql-server
visual-studio
gridview
webforms
【解决方案1】:
您可以为此使用 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++;
}
}
}