基于Gridview的RowDataBound事件中的行数据的单元格条件输出 [英] Conditional output in cell based on row data in Gridview's RowDataBound event

查看:14
本文介绍了基于Gridview的RowDataBound事件中的行数据的单元格条件输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个位值(黑色)我想在gridview中显示它的状态,好像它是真的,行显示是",否则行显示否",这是我的代码,但结果不正确,因为我的代码显示所有行是"如果一个值为真,我想显示每一行状态

i have a bit value (Black) i want to display its status in gridview as if its true, the row display "Yes", otherwise the row display "No", this is my code, but the result is not right, cuz my code display all rows "Yes" if one value is true, i want to display each row status

    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";
    }
}

这篇关于基于Gridview的RowDataBound事件中的行数据的单元格条件输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆