1 votes

Get DropdownList control value in Gridview

I am presenting a small inconvenience at the time of trying to get a value from a dropdownlist control

I have the following scenarios

  1. I think my list control and food through this way
<tr>
                         <td style="width: 382px">BODEGA DE RETORNO</td>
                         <td>
                             <asp:DropDownList ID="DropDownListBodega" runat="server" Height="16px" Width="146px">
                             </asp:DropDownList>
                         </td>
                      </tr>

codebehind

Usuario UsuSession = (Usuario)Session["USUARIO_SESSION"];
var obtenerUsuario = db.usuarios.Where(u => u.id_usuario == UsuSession.IdUsuario);
                var objusuario = obtenerUsuario.FirstOrDefault();

                var obtenerBodegas = modelo.bodega.Where(b => b.id_municipio == objusuario.id_municipio);
                var bodega = obtenerBodegas.ToList();
                DropDownListBodega.DataSource = bodega;
                DropDownListBodega.DataTextField = "Nombre";
                DropDownListBodega.DataValueField = "id_bodega";
                DropDownListBodega.DataBind();
                DropDownListBodega.Items.Insert(0, "Select");

until here it works normal. Do I get the values in the list that I want

  1. I have a DataGrid in which I show a set of data with which I intend to perform an action with each one of them, the food by using query
<asp:GridView ID="GridViewDatos" runat="server" AutoGenerateColumns="False" Width="100%" DataKeyNames="id_factura,id_detalle_factura,id_factura_articulo" CssClass="table table-striped table-bordered table-hover">
                        <Columns>
                            <asp:BoundField DataField="id_factura_articulo" HeaderText="Id Factura Articulo" />
                            <asp:BoundField DataField="id_factura" HeaderText="ID Factura" />
                            <asp:BoundField DataField="id_detalle_factura" HeaderText="Detalle Factura" /> 
                            <asp:BoundField DataField="nombre_articulo" HeaderText="Nombre Articulo" /> 
                            <asp:BoundField DataField="nombre_bodega" HeaderText="Bodega" />
                            <asp:BoundField DataField="serial" HeaderText="Serial" />
                            <asp:BoundField DataField="codigobarras" HeaderText="Código Barras" />
                            <asp:HyperLinkField Text="Devolver" DataNavigateUrlFields="id_factura,id_detalle_factura,id_factura_articulo" DataNavigateUrlFormatString="DetalleDevolucion.aspx?idFactura={0}&idDetalle={1}&id_factura_articulo={2}&proceso=Devolver" /> 
                        </Columns>
                        <EmptyDataTemplate>
                            No se encontraron datos
                        </EmptyDataTemplate>
                    </asp:GridView>

codebehind

decimal idFactura = Convert.ToDecimal(factura.id_factura);
            System.Text.StringBuilder sql = new System.Text.StringBuilder();
            sql.Append("SELECT fa.id_factura_articulo, df.id_factura,fa.id_detalle_factura,ar.nombre as nombre_articulo, fa.serie as serial,bd.nombre as nombre_bodega,ar.codigobarras FROM factura_articulo fa ");
            sql.Append("INNER JOIN detalle_factura df ON fa.id_detalle_factura = df.id_detalle_factura ");
            sql.Append("INNER JOIN bodega bd ON bd.id_bodega = df.id_bodega ");
            sql.Append("INNER JOIN articulo ar ON ar.articuloid = fa.id_articulo  ");
            sql.Append("WHERE fa.id_factura = @p0 ");

            var registros = db.Database.SqlQuery<FacturaArticulo>(sql.ToString(), idFactura).ToList();

            GridViewDatos.DataSource = registros;

            GridViewDatos.DataBind();

In the dataGrid I have a control type HyperLinkField with which to give click a record I intend to perform actions with the same

All of the above shows me the data as I want to see

My drawback is present at the time of wanting to get the selected value in the dropdownlist once given-click on the control, HyperLinkField I am getting the same value always and is not the value that I select

In the load I set up a validation to determine whether to perform a process with the records in the datagrid, or if the contrary will not be made actions

if (Request.QueryString["proceso"] != null)
 {
  decimal bodVenta = Convert.ToDecimal(DropDownListBodega.SelectedValue);// en este punto el valor siempre me llega igual
}

I request you to guide me a little or what should I modify so that I do not get the default value if not the one I selected

1voto

Leandro Tuttini Points 25288

To take the value of the DropDownList you would have to use the event RowCommand

Using CommandField in a GridView control

<asp:GridView ID="GridViewDatos" runat="server" AutoGenerateColumns="False" Width="100%" DataKeyNames="id_factura,id_detalle_factura,id_factura_articulo" CssClass="table table-striped table-bordered table-hover">
<Columns>
    <asp:BoundField DataField="id_factura_articulo" HeaderText="Id Factura Articulo" />
    <asp:BoundField DataField="id_factura" HeaderText="ID Factura" />
    <asp:BoundField DataField="id_detalle_factura" HeaderText="Detalle Factura" /> 
    <asp:BoundField DataField="nombre_articulo" HeaderText="Nombre Articulo" /> 
    <asp:BoundField DataField="nombre_bodega" HeaderText="Bodega" />
    <asp:BoundField DataField="serial" HeaderText="Serial" />
    <asp:BoundField DataField="codigobarras" HeaderText="Código Barras" />
    ButtonType="Link" ShowSelectButton ="true"  />
</Columns>

special attention to the <asp:BoundField

CommandField.ShowSelectButton Property

since then you used the event

void GridViewDatos_SelectedIndexChanged(Object sender, EventArgs e)
{
    GridViewRow row = GridViewDatos.SelectedRow;

    string id_factura = GridViewDatos.DataKeys[row.RowIndex].Values["id_factura"];
    string id_detalle_factura = GridViewDatos.DataKeys[row.RowIndex].Values["id_detalle_factura"];
    //resto

    decimal bodVenta = Convert.ToDecimal(DropDownListBodega.SelectedValue);

}

alli if you're going to be able to access the value of the combo because it was in a event of the page and not in a menu of a link

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X