Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

e.Row.DataItem error in gridview rowdatabound event

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.

like image 203
rockenpeace Avatar asked Jan 14 '14 12:01

rockenpeace


People also ask

What is RowDataBound event in GridView?

The RowDataBound event is raised when a data row (represented by a GridViewRow object) is bound to data in the GridView control. This enables you to provide an event-handling method that performs a custom routine, such as modifying the values of the data bound to the row, whenever this event occurs.

How can get GridView column value in RowDataBound?

Get cell value of GridView in RowCommand event in ASP.Net The row index can be easily determined using the CommandArgument property of GridViewCommandEventArgs object and using the row index, the GridView Row is referenced. Finally using the reference of the GridView Row, the values from the cells are fetched.


2 Answers

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;
                }
            }
        }
    }
}
like image 147
rockenpeace Avatar answered Sep 27 '22 15:09

rockenpeace


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

            }
        }
    }
}
like image 31
Secret Squirrel Avatar answered Sep 27 '22 16:09

Secret Squirrel