0
votes

im trying to find a solution to my problem, my dropdownlist in the gridview is not binding, is there any way to override this?

this is the error i am getting:

Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

thanks

   <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
                DataSourceID="SqlDataSource22" EnableModelValidation="True" ForeColor="#333333"
                Width="2400px" DataKeyNames="KeyFeatureID">
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" HorizontalAlign="Center"
                    VerticalAlign="Middle" />
    <Columns>
         <asp:TemplateField HeaderText="KeyFeatureID" SortExpression="KeyFeatureID" Visible="False">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("KeyFeatureID") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("KeyFeatureID") %>'></asp:Label>
            </ItemTemplate>
            <HeaderStyle Width="60px" />
         </asp:TemplateField>
         <asp:TemplateField HeaderText="Category">
            <EditItemTemplate>
                <asp:DropDownList ID="DDlL" runat="server" EnableViewState="False" Style="font-size: x-small"
                                Width="200px" AppendDataBoundItems="True" DataSourceID="SqlDataSource11104" DataTextField="Category"
                                DataValueField="Category" SelectedValue='<%# Bind("Category") %>'>
                    <asp:ListItem>Category</asp:ListItem>
                </asp:DropDownList>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server" Text='<%# Bind("Category") %>'></asp:Label>
            </ItemTemplate>
         </asp:TemplateField>
    </Columns>
   </asp:GridView>
1
I don't know if you can bind a DropDownList in a gridview like that. It looks as though you are trying to call some sort of Sql data source to populate it. Which seems a bit of an overkill - are you making a call to the database to fill the ddl in every row of the grid? For dropdownlists in Gridviews I populate a DataSet that contains the list of ddl items - before binding the GridView and then, in the RowDataBound event of the GridView, find the ddl and bind it to the DataSet. Works fine. If you need to set the selected index using some value in your main gridview dataset, you can. - Martin Smellworse

1 Answers

1
votes

You have to bind your DropDownList in RowDataBound handler.

  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //Bind DropDownList here

        }
     }