0

Hi I have a List which returns an array of "Question". My question is how can I bind this to a grid view? When I try to call Question.Ordinal I get that it does not exist in the data source. I am using the following code:

GridView1.DataSource = myList.GetQ();
GrdiView1.DataBind();

myList.GetQ() returns a List which is an array of "Question".

When I set the column DataField to "!" I get the object Question. My question is how can I get the objects property? I tried "!.Ordinal" does not work. I was reading this post for reference, here, any help is greatly appreciated, thanks.

3 Answers 3

1

Try using the following syntax:

<%# ((MyObject)Container.DataItem).MyField %>
Sign up to request clarification or add additional context in comments.

1 Comment

I get a parse error: Databinding expressions are only supported on objects that have a DataBinding event. System.Web.UI.WebControls.BoundField does not have a DataBinding event.
1

Just set it directly to Ordinal, as the first examples in the post you just linked to:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
   <asp:BoundField HeaderText="ID" DataField="CustId" />
   <asp:BoundField HeaderText="Name" DataField="Name" />
   <asp:BoundField HeaderText="City" DataField="City" />
</Columns>
</asp:GridView>

Say:

<asp:BoundField HeaderText="A Header" DataField="APropertyOfQuestion" />

Comments

1

you must be define the property member of class, as a propery ie

public string ProductName
{
    get
    {
        return _productName;
    }
    set { }

}

Or VB

public property ProductName() as string set ..

get...

end property

important: Is Required defined get method

Comments

Your Answer

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