5

enter image description here I want to enable or disable a particular row according to the related user logined. So i use rowdatabound event but i have an error in this line:

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

In here, e.Row.DataItem has related row information. I have controlled and it has values of row. But when i want to continue, i take this error:

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'.

Then I have changed this line:

DataRowView drv = e.Row.DataItem as DataRowView;

For this condition, it gives no error but drv has still null value. Dataitem does not assign its value.

In here, related full code:

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

What can i do for this error ? Thanks in advance.

5
  • It's not a DataRowView as you've already noticed. Choose a different datasource than an anonymous type and cast it correctly. What is the DataSource currently? Commented Jan 14, 2014 at 12:57
  • what's wrong with DataRow drv = e.Row? Commented Jan 14, 2014 at 12:59
  • for better performance, the if statement should be at the top of this method. before you fetch all those CARGIs Commented Jan 14, 2014 at 13:02
  • in fact why are you fetching on each RowDataBound that looks like a query that should have been executed outside of this method completely. Commented Jan 14, 2014 at 13:03
  • i have edited by adding picture to show dataitem for datasource. Commented Jan 14, 2014 at 13:33

2 Answers 2

11

I have solved my problem two days ago. I've used this:

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

All the event:

    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

Here is a few suggestions which may help fix your issue.

firstly where you select var c I would place this outside of this method. for now I will use a property

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

            }
        }
    }
}
2
  • i take this error : Cannot implicitly convert type 'System.Web.UI.WebControls.GridViewRow' to 'System.Data.DataRow' for DataRow dr = e.Row; Commented Jan 14, 2014 at 13:28
  • For Cagris c = (Cagris)e.Row.DataItem line, i take this error: 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 'CAGRI'. Commented Jan 14, 2014 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.