Binding an ASP.NET GridView Control to a string array

32,561

Solution 1

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>

Solution 2

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

Solution 3

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

Solution 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
    Language="C#" 
    AutoEventWireup="true" 
    CodeBehind="Default.aspx.cs" 
    Inherits="WebApplication2._Default"
%>
<%@Import
    Namespace="System.Collections.Generic"
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>

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

So as a GridView the inner section would be

    <asp:GridView runat="server" ID="Grid1" AutoGenerateColumns="false">
        <Columns>
            <asp:BoundField DataField="!" DataFormatString="Data: {0}"  HeaderText="Dyad"/>
        </Columns>
    </asp:GridView>

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.

Solution 5

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.

Share:
32,561
Michael Kniskern
Author by

Michael Kniskern

I am currently working as an IT engineer for the government of Mesa, Arizona USA

Updated on July 09, 2022

Comments

  • Michael Kniskern
    Michael Kniskern almost 2 years

    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

  • Michael Kniskern
    Michael Kniskern almost 15 years
    Thanks, I like this solution using LINQ.
  • Yousaf
    Yousaf almost 15 years
    Thank you, good point! I have now updated the code to use the correct name.
  • R.L.
    R.L. about 14 years
    thanks. for some reason I can NEVER remember the Container.DataItem syntax :(
  • Andy
    Andy almost 12 years
    hmm, just tried it in a select box (drop-down) and it didn't work :( I wonder if that is specific to GridView. This seems such an obvious general requirement of data binding that I'm really surprised there isn't a proper answer
  • Colin Pear
    Colin Pear over 11 years
    Wow. Do you have a link to where you found this info?
  • Si8
    Si8 over 8 years
    How can I use something like that above in here: stackoverflow.com/questions/34209825/… Thanks.
  • good-to-know
    good-to-know over 7 years
    Still helpful in 2016-2017!