【问题标题】:Cannot cast DataItem to DataRowView in Custom Binding Scenario无法在自定义绑定方案中将 DataItem 强制转换为 DataRowView
【发布时间】:2008-11-07 05:41:50
【问题描述】:

我有一个对象,它返回一个 IList,我从我的 ObjectDataSource 获取并绑定到一个 Gridview。如果我只使用标准绑定,一切正常,但我正在尝试自定义绑定以在链接按钮上设置属性,如下所示:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //  extract the link button
                LinkButton lnkViewMap = (LinkButton)e.Row.FindControl("lnkViewMap");

                //  grab the datarowview
               System.Data.DataRowView row = (System.Data.DataRowView)e.Row.DataItem;

                //  set the onclientclick to fire our showMap javascript function,
                //  passing through the lat/longs
                lnkViewMap.OnClientClick = string.Format("showMap({0}, {1}); return false;", row["Lat"], row["Long"]);
            }
        }

我将 e.Row.DataItem 转换为 DataRowView 时发生错误。上面的代码来自 Matt Berseth 在 Virtual Earth 上的精彩博客……这就是我在这里尝试实现的。有什么想法吗?

【问题讨论】:

    标签: c# data-binding gridview


    【解决方案1】:

    在调试器中设置断点,看看e.Row.DataItem到底是什么类型。

    只有当您在网格上设置的DataSourceDataViewDataTable 时,它才会是DataRowView。否则它将是集合的元素类型。

    【讨论】:

    • 做到了...我将 DataItem 转换为我的元素类型,然后使用普通表示法访问它的属性,它起作用了...谢谢!
    • NP .. 加上这比乱用 DataRowViews 好得多!强类型数据绑定 FTW。
    猜你喜欢
    • 2010-12-17
    • 2019-02-02
    • 1970-01-01
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    • 1970-01-01
    • 2013-05-09
    • 1970-01-01
    相关资源
    最近更新 更多