How can I call a DropDownList within a Gridview?


I have a gridview in Default.aspx that contains drop-down lists, in Default.cs I can not use them, they do not appear to me, it's as if they were not. How do I call them?

Image of the mention I make.


The Estroctura in Default.aspx

<asp:GridView ID="gvDetails" runat="server" EmptyDataText="No se han encontrado registros para mostrar."  AutoGenerateColumns="False" CssClass="auto-style5">
            <asp:DropDownList ID="ddlProjgvw" runat="server" 
            OnSelectedIndexChanged = "Filtro" AutoPostBack = "true"
            AppendDataBoundItems = "true">
            <asp:ListItem Text = "ALL" Value = "ALL"></asp:ListItem>
            <%# Eval("NameProj") %> 
asked by KJSK 23.11.2018 в 01:28

2 answers


what you should do is get the row in which the DropDownList is located:

Dim row As GridViewRow = DirectCast(DirectCast(sender, DropDownList).NamingContainer, GridViewRow);

to then be able to search inside the row for the nested control:

DirectCast(row.FindControl("ddlProveedor"), DropDownList).DataSource=cmd.ExecuteReader();
DirectCast(row.FindControl("ddlProveedor"), DropDownList).DataTextField = ""
DirectCast(row.FindControl("ddlProveedor"), DropDownList).DataValueField = ""

I hope it's helpful.

answered by 24.11.2018 / 17:05

The first thing you should do is save the query in a variable outside the function that you can access to fill all the DropDownList of the grid without having to make a query for each row.

DataTable dtPrioridades = new DataTable();

private void FillPrioridadList()
    SqlConnection sqlCon = new SqlConnection(connectionString);
    if (sqlCon.State == ConnectionState.Closed)
    SqlCommand cmd = new SqlCommand("ddlPrioridad", sqlCon);

After that you have to access the DropDownList of each row. You can do it either by going through the rows with a foreach or with the function RowCreated of GridView . There are many ways to do it but the important thing is that you must go row by row on GridView .

protected void grd_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    if(e.Row.RowType == DataControlRowType.DataRow)
        ((DropDownList)e.Row.FindControl("ddlPrioridadgvw")).DataSource = dtPrioridades;
        ((DropDownList)e.Row.FindControl("ddlPrioridadgvw")).DataTextField = "P_Nivel";
        ((DropDownList)e.Row.FindControl("ddlPrioridadgvw")).DataValueField = "IdPrioridad";
answered by 24.11.2018 в 18:07