我有一点值(黑色)我想在gridview中显示它的状态,好像它是真的,行显示“是”,否则行显示“否”,这是我的代码,但结果不对,因为我的代码显示所有行“是”如果一个值为true,我想显示每一行的状态
protected void gridview1_RowDataBound(object sender,GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { DataTable dt = GetData(); for (int i = 0; i < dt.Rows.Count; i++) { Boolean bitBlack = Convert.ToBoolean(dt.Rows[i]["Black"]); if (bitBlack) { e.Row.Cells[7].Text = ("Yes"); } else { e.Row.Cells[7].Text = ("No"); } } } }
解决方法
您始终可以使用行
DataItem
来获取基础DataSource:
protected void gridview1_RowDataBound(object sender,GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { DataRow row = ((DataRowView)e.Row.DataItem).Row; bool isBlack = row.Field<bool>("Black"); e.Row.Cells[7].Text = isBlack ? "Yes" : "No"; } }