Can't put Dropdownlist into a Datagrid

173 views Asked by At

Right now I have a Datagrid displaying some details for trailers. I want to put a DropDownList into the Datagrid so the location can be changed but I keep getting the error:

Error 32 Literal content ('') is not allowed within a 'System.Web.UI.WebControls.DataGridColumnCollection'. C:\inetpub\wwwroot\test\test\lookups\Trailer.aspx 215

I've seen code examples of of DropDownLists in datagrids before so why am I getting this error? Here's the code for the DataGrid:

      <asp:DataGrid runat="server" CssClass="tblResults" OnItemDataBound="dgList_ItemCreated" AllowSorting="true" OnSortCommand="dgTrailer_Sort" ID="dgTrailers" DataKeyField="ID" AutoGenerateColumns="false">
                <HeaderStyle CssClass="tblResultsHeader" />
                <AlternatingItemStyle BackColor="#EEEEEE" />
                <Columns>
                    <asp:BoundColumn DataField="TrailerMake" HeaderText="Trailer Make" SortExpression="TrailerMake"></asp:BoundColumn>
                    <asp:BoundColumn DataField="TrailerLocation" HeaderText="Trailer Location" SortExpression="TrailerLocation"></asp:BoundColumn>
                    <asp:BoundColumn DataField="Year" HeaderText="Year" SortExpression="Year"></asp:BoundColumn>

2

There are 2 answers

1
d-unit On

Firstly, it looks like you are missing the closing tag for Columns.

Secondly, to add a dropdownlist you would have to use asp:TemplateColumn then bind your data to that dropdownlist:

<asp:DataGrid runat="server" CssClass="tblResults" OnItemDataBound="dgList_ItemCreated" AllowSorting="true" OnSortCommand="dgTrailer_Sort" ID="dgTrailers" DataKeyField="ID" AutoGenerateColumns="false">
            <HeaderStyle CssClass="tblResultsHeader" />
            <AlternatingItemStyle BackColor="#EEEEEE" />
            <Columns>
                <asp:BoundColumn DataField="TrailerMake" HeaderText="Trailer Make" SortExpression="TrailerMake"></asp:BoundColumn>
                <asp:BoundColumn DataField="TrailerLocation" HeaderText="Trailer Location" SortExpression="TrailerLocation"></asp:BoundColumn>
                <asp:BoundColumn DataField="Year" HeaderText="Year" SortExpression="Year"></asp:BoundColumn>
                <asp:TemplateColumn><ItemTemplate><asp:DropDownList ID="ddlList" runat="server" /></ItemTemplate></asp:TemplateColumn>
            </Columns>
        </asp:DataGrid>
1
Gilbert Adjin Frimpong On

First you will have to convert the column to a template, then you can add a dropdownlist into this template and bind your data to the dropdownlist. in the end the column with the dropdown will look like the one below.

<Columns>
   <asp:TemplateField>                 
     <ItemTemplate>
          <asp:DropDownList ID="DropDownList1" runat="server" Height="32px" Width="178px">
        </asp:DropDownList>
     </ItemTemplate>
 </asp:TemplateField>             
 </Columns>