Home
C# Bind Array To Repeater/DataGrid
Blog Date 6 May 2021
Code
string[] MyArray = {"The", "dog", "ran", "around", "the", "park"};
MyGrid.DataSource = MyArray;
MyGrid.DataBind();
HTML
<asp:Repeater ID="MyGrid" runat="server">
<%# Container.DataItem %>
</asp:Repeater>
Explain
I'm so used to using repeaters (I prefer them to DataGrids as I control the HTML) when binding data from databases I'd never come across binding an array to a repeater (or DataGrid).
I feared the worst, complex table creations, conversions to and from unknown data types and fearfully convoluted swathes of code. Nope. You can just bind the array directly to the Repeater and use Container.DataItem to loop through the array's items. Errr, don't forget the hash in <%# Container.DataItem %>
Wanna give Ren a C# job? Contact ren@techsolus.co.uk
Reader's Comments
Name
Comment
Add a RELEVANT link (not required)
Upload an image (not required)
Uploading...
Home