I'm using an ObjectDataSource to bind an object to a GridView. In the OnRowDataBound event handler I'm trying to determine if a certain button should be visible or not. When the runtime encounters this statement it fires an "No default member found for type 'Pledge'." error:
lbDel.Visible = Not (e.Row.DataItem("BillingReady"))
My Object class that is bound to the GridView:
public class Pledges : System.Collections.CollectionBase
{
public Pledge this[int index]
{
get { return ((Pledge)(List[index])); }
set { List[index] = value; }
}
public int Add(Pledge pledge)
{
return List.Add(pledge);
}
}
My Pledge class:
public class Pledge
{
public int PledgeID { get; set; }
public int EventID { get; set; }
public int SponsorID { get; set; }
public int StudentID { get; set; }
public decimal Amount { get; set; }
public string Type { get; set; }
public bool IsPaid { get; set; }
public string EventName { get; set; }
public DateTime EventDate { get; set; }
public bool BillingReady { get; set; }
public string SponsorName { get; set; }
public int Grade_level { get; set; }
public string StudentName { get; set; }
public string NickName { get; set; }
public int Laps { get; set; }
public decimal PledgeSubtotal { get; set; }
}
My OnRowDataBound event handler:
Protected Sub PledgeGrid_OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If (e.Row.RowType = DataControlRowType.DataRow) And _
Not ((e.Row.RowState = DataControlRowState.Edit) Or ((e.Row.RowState = DataControlRowState.Alternate) And (e.Row.RowState = DataControlRowState.Edit))) Then
Dim lbDel As LinkButton
Dim lbEd As LinkButton
lbDel = CType(e.Row.FindControl("lbDelete"), LinkButton)
lbEd = CType(e.Row.FindControl("lbEdit"), LinkButton)
If ((e.Row.RowState = DataControlRowState.Normal) Or (e.Row.RowState = DataControlRowState.Alternate)) Then
lbDel.Visible = Not (e.Row.DataItem("BillingReady")) '<-- Problem happens here
lbEd.Visible = Not (e.Row.DataItem("BillingReady"))
End If
End If
End Sub
Yes, I'm having to mix VB and C# but I don't think that's the problem. If I understand the C# equivalent of a VB Default Property is called an indexer. Shouldn't this qualify as an indexer?
public Pledge this[int index]
{
get { return ((Pledge)(List[index])); }
set { List[index] = value; }
}
Try to cast the DataItem
to Pledge
:
Dim pledge = DirectCast(e.Row.DataItem, Pledge)
lbDel.Visible = Not pledge.BillingReady
I had this same problem when binding a c# model to a VB DataGrid.
Normally in the aspx file, the DataGrid would display a field in a template column with:
<asp:templatecolumn headertext="Reference">
<itemtemplate>
<%# Container.DataItem("Reference")%>
</itemtemplate>
</asp:templatecolumn>
But when using my c# model, the DataGrid needs a template column syntax of:
<asp:templatecolumn headertext="Reference">
<itemtemplate>
<%# DataBinder.Eval(Container.DataItem, "Reference") %>
</itemtemplate>
</asp:templatecolumn>
This was a confusing issue so hopefully the answer will help you save the hour it took me to figure this out.