Binding an ASP.NET GridView Control to a string array
- by Michael Kniskern
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 scenarion