views:

2217

answers:

8

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

+3  A: 

Try replacing the BoundField with a TemplateField like so:

<asp:TemplateField HeaderText="String Value">
        <ItemTemplate>
            <%# Container.DataItem %>
        </ItemTemplate>
    </asp:TemplateField>

BTW I lifted this from another question

Matthew Jones
thanks. for some reason I can NEVER remember the Container.DataItem syntax :(
R.L.
+1  A: 

One method is to pass it a class with a single, named field. That way, you can give it a name.

public class GridRecord
{
    public string MyValue { get; set; }
}

Then convert your string array to a list of the class

string[] MyArray = new string[1];
MyArray[0] = "My Value";
List<GridRecord> MyList = (
    from ar in myArray
    select new GridRecord
    {
     MyValue = ar
    }).ToList();
MyGridView.DataSource = MyList;
MyGridView.DataBind();

Now you can name your DataField property

<asp:GridView ID="MyGridView" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="MyValue" />
    </Columns>
</asp:GridView>
Michael La Voie
Thanks, I like this solution using LINQ.
Michael Kniskern
A: 

Michael,

The line of code

<asp:BoundField DataField="Item" />

expects a column with the name of "Item," which you would have if you were binding to one of the DataSource controls such as SqlDataSource, ObjectDataSource, or LinqDataSource. Since you are binding to an IEnumerable, you have no such name.

Robert Harvey
Thank you, good point! I have now updated the code to use the correct name.
Michael La Voie
A: 

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

Luk
A: 

Luk

Thank you very much for your contribution. It has just helped me - I was trying to bind a GridView to an array, exactly the same as the OP and this has solved it in one second!

Mountford D
A: 

You ABSOLUTE legend. I'd buy you a beer if you were in the same bar as me at any point. Not an obvious solution this one!

JConaBike
A: 

You are an absolute legend!!!!!!!!!! That worked a treat and such a simple solution.

i_of_sauron