在细胞条件输出基础上,GridView的RowDataBound事件行数据 [英] Conditional output in cell based on row data in Gridview's RowDataBound event

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

问题描述

我有一个位值(黑色)我想显示为如果属实,该行显示是它在GridView的状态,否则该行显示否,这是我的code,但结果是不对的,我的Cuz code显示所有行是如果一个值是真的,我想显示每行状态。

 保护无效gridview1_RowDataBound(对象发件人,GridViewRowEventArgs E)
    {
        如果(e.Row.RowType == DataControlRowType.DataRow)
        {
            DataTable的DT =的GetData();
            的for(int i = 0; I< dt.Rows.Count;我++)
            {
                布尔bitBlack = Convert.ToBoolean(dt.Rows [I] [黑]);
                如果(bitBlack)
                {
                    e.Row.Cells [7]。文本=(是);
                }
                其他
                {
                    e.Row.Cells [7]。文本=(否);
                }
            }
        }
    }


解决方案

您可以随时使用的行<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewrow.dataitem.aspx\"><$c$c>DataItem得到基本数据源

 保护无效gridview1_RowDataBound(对象发件人,GridViewRowEventArgs E)
{
    如果(e.Row.RowType == DataControlRowType.DataRow)
    {
        行的DataRow =((DataRowView的)e.Row.DataItem).Row;
        布尔isBlack = row.Field&LT;布尔&GT;(黑);
        e.Row.Cells [7]。文本= isBlack? 是:否;
    }
}

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

解决方案

You could always use the rows DataItem to get the underlying 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天全站免登陆