Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to bind an ASP.NET GridView control to an string array and I get the following item:

A field or property with the name 'Item' was not found on the selected data source.

What is correct value I should use for DataField property of the asp:BoundField column in my GridView control. Here is my source code:

ASPX page

<asp:GridView ID="MyGridView" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Item" />
        <asp:CommandField ButtonType="Link" ShowSelectButton="true" SelectText="Click Me!" />
    </Columns>
</asp:GridView>

Code Behind:

string[] MyArray = new string[1];
MyArray[0] = "My Value";
MyGridView.DataSource = MyArray;
MyGridView.DataBind();

UPDATE

I need to have the AutoGenerateColumns attribute set to false because I need to generate additional asp:CommandField columns. I have updated my code sample to reflect this scenario

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
312 views
Welcome To Ask or Share your Answers For Others

1 Answer

After hours of search, I finally found that there is a special DataField for this case: "!"

<asp:GridView ID="MyGridView" runat="server" AutoGenerateColumns="false">
<Columns>
    <asp:BoundField DataField="!" />
</Columns>
</asp:GridView>

I hope it'll help someone one day :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...