在GridView的RowDataBound事件中出现e.Row.DataItem错误

5
我希望能根据相关用户的登录情况启用或禁用特定行。因此,我使用了rowdatabound事件,但是在这一行中出现了错误:

enter image description here

DataRow drv = ((DataRowView)e.Row.DataItem).Row; 

在这里,e.Row.DataItem具有相关的行信息。我已经进行了控制,并且它具有行的值。但是当我想要继续时,我会遇到这个错误:
Unable to cast object of type '<>f__AnonymousType014[System.Int32,System.Int32,System.String,System.String,System.DateTime,System.String,System.String,System.String,System.String,System.String,System.String,System.Nullable1[System.DateTime],System.String,System.Nullable`1[System.Boolean]]' to type 'System.Data.DataRowView'。
然后我更改了这一行:
DataRowView drv = e.Row.DataItem as DataRowView;

对于这种情况,虽然没有出现错误,但drv仍然是null值。Dataitem并没有分配它的值。

在这里,相关的完整代码:

protected void gvListele_RowDataBound(object sender, GridViewRowEventArgs e)
{
    dbeDataContext db = new dbeDataContext();
    var c = (from v in db.CAGRIs where v.UserID != Convert.ToInt32(Session["user"]) select v).ToArray();
    if (c != null)
    {
        foreach (var item in c)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                 DataRow drv = ((DataRowView)e.Row.DataItem).Row; 

                int tempID = Convert.ToInt32(drv["CagriID"].ToString());
                if (item.CagriID == tempID)
                {
                    e.Row.Enabled = false;
                }
            }
        }
    }
}

我该如何解决这个错误?先感谢您的帮助。

正如您已经注意到的那样,它不是 DataRowView。选择一个不同于匿名类型的数据源并进行正确的转换。目前的 DataSource 是什么? - Tim Schmelter
DataRow drv = e.Row 有什么问题吗? - Squirrel5853
为了获得更好的性能,此方法顶部应该放置if语句,在获取所有这些CARGI之前。 - Squirrel5853
事实上,为什么你要在每个RowDataBound中进行获取,看起来像是一个应该在此方法之外完全执行的查询。 - Squirrel5853
我已经编辑过了,添加了图片以展示数据源的数据项。 - rockenpeace
2个回答

11

我两天前解决了我的问题。我使用了这个:

int callID = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "CagriID"));

所有事件:

    protected void gvListele_RowDataBound(object sender, GridViewRowEventArgs e)
{
    dbeDataContext db = new dbeDataContext();
    var c = (from v in db.CAGRIs where v.UserID != Convert.ToInt32(Session["user"]) select v).ToArray();
    if (c != null)
    {
        foreach (var item in c)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int callID = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "CagriID"));
                if (callID == item.CagriID)
                {
                    e.Row.Enabled = false;
                    continue;
                }
            }
        }
    }
}

1
这里有一些建议,可能有助于解决您的问题。
首先,在选择var c时,我会将其放在该方法之外。现在我将使用一个属性。
public object Cagris
{  
    get
    {
        if(_cagris == null)
        {
             _cagris =  (from v in db.CAGRIs 
                         where v.UserID != Convert.ToInt32(Session["user"]) // the session variable I would also have as a property rather than access it each time like this. 
                         select v).ToArray();
        }
        return _cagris;
    }
}


protected void gvListele_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {

        if (Cargis != null)
        {
            foreach (var item in Cargis)
            {
                //now you can either get the DataRow  
                DataRow dr = e.Row;
                int tempID = int.Parse((string)dr["CagriID"]);

                if (item.CagriID == tempID)
                {
                    e.Row.Enabled = false;
                }


                //or use the DataItem
                Cagris c = (Cagris)e.Row.DataItem;

                if (item.CagriID == c.CagriID)
                {
                    e.Row.Enabled = false;
                }

            }
        }
    }
}

我遇到了这个错误:无法将类型'System.Web.UI.WebControls.GridViewRow'隐式转换为类型'System.Data.DataRow',因为代码DataRow dr = e.Row;。 - rockenpeace
对于 Cagris c = (Cagris)e.Row.DataItem 这一行,我遇到了这个错误:无法将类型为 '<>f__AnonymousType014[System.Int32,System.Int32,System.String,System.String,System.DateTime,System.String,System.String,System.String,System.String,System.String,System.String,System.Nullable1[System.DateTime],System.String,System.Nullable`1[System.Boolean]]' 的对象强制转换为类型 'CAGRI'。 - rockenpeace

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接