user2910569
user2910569

Reputation: 21

gridview binding problems with dropdownlist

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>

Upvotes: 0

Views: 196

Answers (1)

Nanosoft
Nanosoft

Reputation: 119

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

        }
     }

Upvotes: 1

Related Questions