List dropdown in editItemTemplate and footerTemplate in gridview

I have a grid where I am trying to associate a drop down list with FooterTemplate and EdiItemTemplate.

<asp:TemplateField HeaderText="Role" SortExpression="Role">
                                    <HeaderStyle Width="100px"   HorizontalAlign="Center" Wrap="False"></HeaderStyle>
                                    <EditItemTemplate>
                                        <asp:DropDownList ID="ddlRole" runat="server">
                                        </asp:DropDownList>
                                    </EditItemTemplate>
                                    <FooterTemplate>
                                        <asp:DropDownList ID="ddlFRRole" runat="server">
                                        </asp:DropDownList>
                                    </FooterTemplate>
                                    <ItemTemplate>
                                        <asp:Label ID="lblRole" runat="server" Text='<%# Bind("[Role_Cd]") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>

I use this code and no luck. I cannot bind values ​​to a dropdown. Can someone fix me if I am wrong?

protected void UPGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList ddlRole = (DropDownList)e.Row.FindControl("ddlRole");
            DropDownList ddlFRRole = (DropDownList)e.Row.FindControl("ddlFRRole");
            ddlRole.DataSource = UPRepository.GetRoles();                
            ddlRole.DataTextField = "Role_Cd";
            ddlRole.DataValueField = "Role_Cd";
            ddlRole.DataBind();
            ddlFRRole.DataSource = UPRepository.GetRoles();                
            ddlFRRole.DataTextField = "Role_Cd";
            ddlFRRole.DataValueField = "Role_Cd";
            ddlFRRole.DataBind();

        }
    }
+4
source share
1 answer

First you have to check and compare RowIndexwith the GridView EditIndexyou need to edit, and put all the code associated with the dropdown menu inside.

In addition, to search for a drop-down list in the footer, you need to use the condition:

if (e.Row.RowType == DataControlRowType.Footer)

And to search the list in the line use

if (e.Row.RowType == DataControlRowType.DataRow)

RowDataBound GridView. :

protected void MyGridView_RowDataBound( object sender, GridViewRowEventArgs e)
    {
        if(MyGridView.EditIndex == e.Row.RowIndex ) //GET THE ROW TO BE EDITED
         {
            if (e.Row.RowType == DataControlRowType.DataRow)
              {
               DropDownList ddlRole = (DropDownList)e.Row.FindControl("ddlRole");
                // Rest of Code
              }
        if (e.Row.RowType == DataControlRowType.Footer)
         {
           DropDownList ddlFRRole = (DropDownList)e.Row.FindControl("ddlFRRole");
           // Rest of code
         }
    }

- :

if ((e.Row.RowState & DataControlRowState.Edit) > 0)

:: if (e.Row.RowState == DataControlRowState.Edit)

+5

All Articles