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">
        <asp:BoundField DataField="Item" />
        <asp:CommandField ButtonType="Link" ShowSelectButton="true" SelectText="Click Me!" />

Code Behind:
string[] MyArray = new string[1];
MyArray[0] = "My Value";
MyGridView.DataSource = MyArray;


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


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

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

I hope it’ll help someone one day 🙂

Method 2

Try replacing the BoundField with a TemplateField like so:

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

BTW I lifted this from another question

Method 3

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
MyGridView.DataSource = MyList;

Now you can name your DataField property
<asp:GridView ID="MyGridView" runat="server" AutoGenerateColumns="false">
        <asp:BoundField DataField="MyValue" />

Method 4

Here is a complete example using the old DataGrid…so it appears that the “!” trick has widespread implementation. This worked under ASP.NET in VS2008. Of course, just substitute the right element names to use a GridView.

<%@ Page

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

    <script type="text/C#" runat="server">
        void initList()
        List<String> myList = new List<String>();
        Grid1.DataSource = myList;
    <form id="form1" runat="server">
        <%initList(); %>
        <asp:DataGrid runat="server" ID="Grid1" AutoGenerateColumns="false">
                <asp:BoundColumn DataField="!" DataFormatString="Data: {0}"  HeaderText="Dyad"/>

So as a GridView the inner section would be
    <asp:GridView runat="server" ID="Grid1" AutoGenerateColumns="false">
            <asp:BoundField DataField="!" DataFormatString="Data: {0}"  HeaderText="Dyad"/>

If you switch back and forth, notice that VS2008 (at least) cannot re-declare the control type in the Designer.cs class, so you’ll have to change that by hand if just editing the element names.

Method 5


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.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x