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

Categories

0 votes
323 views
in Technique[技术] by (71.8m points)

c# - Binding an ASP.NET GridView Control to a string array

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

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

1 Answer

0 votes
by (71.8m points)

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 :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...