Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I bind a gridview to a Generic list of strings?
    text
    copied!<p>This should be simple but I am losign this battle. I have a gridview. All I want is for the user to be able to enter names into it, click add and have the values added. I also want to use the delete command, should they make a mistake. I have tried Eval and Bind. But something keeps going wrong. </p> <p>I have a textbox in the to enter the first one. And alos a textbox in the footer to enter values. When they are entered, they are added to a generic list (string) and re-bound to the gridview. But it fails when it attempts to bind to the list. </p> <p>In the markup below, DataItem.Alias &lt;- Alias is actually the name of the generic list</p> <pre><code> &lt;asp:GridView ID="gvAlias" runat="server" ShowFooter="True" AutoGenerateColumns="false" ShowHeaderWhenEmpty="True" ShowHeader="False" &gt; &lt;Columns&gt; &lt;asp:TemplateField&gt; &lt;ItemTemplate&gt; &lt;%# DataBinder.Eval(Container, "DataItem.Alias")%&gt; /&gt; &lt;/ItemTemplate&gt; &lt;FooterTemplate&gt; &lt;asp:TextBox ID="txtFooterAlias" runat="server" /&gt; &lt;asp:LinkButton ID="btnFooterAdd" runat="server" OnClick="AddFromFooter"&gt;Add New&lt;/asp:LinkButton&gt; &lt;/FooterTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:CommandField ShowDeleteButton="True" /&gt; &lt;/Columns&gt; &lt;EmptyDataTemplate&gt; &lt;asp:TextBox ID="txtEmptyAlias" runat="server" /&gt; &lt;asp:LinkButton ID="btnEmptyAdd" runat="server" OnClick="AddFromEmpty"&gt;Add New&lt;/asp:LinkButton&gt; &lt;/EmptyDataTemplate&gt; &lt;/asp:GridView&gt; </code></pre> <p>and the code behind...</p> <p>Public Sub BindGridView()</p> <pre><code>Dim oThis As This 'this is a class I have that has a property which is a generic list of string gvAlias.DataSource = oThis.Alias.ToArray gvAlias.DataBind() </code></pre> <p>End Sub</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload