views:

1526

answers:

1

I have an object that returns an IList which I'm getting from my ObjectDataSource and binding to a Gridview. All works fine if I just use standard binding, but I'm trying to customize my binding to set properties on a linkbutton as follows:

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"]);
            }
        }

My error occurs where I am casting the e.Row.DataItem to a DataRowView. The code above is from Matt Berseth's awesome blog on Virtual Earth...which is what I am trying to implement here. Any ideas?

+1  A: 

Set a breakpoint in the debugger and see what type e.Row.DataItem actually is.

It would only be a DataRowView if the DataSource you are setting on the grid is a DataView or DataTable. Otherwise it would be the element type of the collection.

Brannon
That did it...I cast the DataItem to my element type and then accessed it's properties using the normal notation and it worked...thanks!
Webjedi
NP .. plus that's way better than messing around with DataRowViews! Strongly-typed data binding FTW.
Brannon