5

在此处输入图像描述 我想根据登录的相关用户启用或禁用特定行。所以我使用 rowdatabound 事件,但我在这一行有一个错误:

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

在这里,e.Row.DataItem 有相关的行信息。我已经控制并且它具有行值。但是当我想继续时,我会遇到这个错误:

无法将类型为“<>f__AnonymousType0 14[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]]”的对象转换为类型“System.Data.DataRowView”。

然后我改变了这一行:

DataRowView drv = e.Row.DataItem as DataRowView;

对于这种情况,它没有给出错误,但 drv 仍然具有空值。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;
                }
            }
        }
    }
}

我该怎么办这个错误?提前致谢。

4

2 回答 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;
                }
            }
        }
    }
}
于 2014-01-16T14:08:44.040 回答
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;
                }

            }
        }
    }
}
于 2014-01-14T13:12:46.617 回答