fill dropdownlist in gridview and the maximum value must be the value that one of the cells has

0

I have a gridview with products from a store listing some characteristics, among them the quantity of products in stock so the user should not be able to select a value greater than the number in stock (stocks)

I have this gridview and I need to add a column with a DropDownlist and this DropDownList must have values from zero and the maximum value must be the value that is in the Inventory column.

<asp:GridView ID="GridView1" runat="server" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" AutoGenerateColumns="False">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:CheckBox ID="chk" Visible='<%# Convert.ToInt32(Eval("Existencias")) > 0 %>' runat="server" OnCheckedChanged="chk_CheckedChanged" AutoPostBack="true" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField HeaderText="Codigo" DataField="IdProducto"/>
        <asp:BoundField HeaderText="Marca" DataField="NomMarca"/>
        <asp:BoundField HeaderText="Ciudad" DataField="NomCiudad"/>
        <asp:BoundField HeaderText="Tamaño" DataField="Tamano"/>
        <asp:BoundField HeaderText="Fragilidad" DataField="NomFragilidad"/>
        <asp:BoundField HeaderText="Ubicacion" DataField="NomUbicacion"/>
        <asp:BoundField HeaderText="Proveedor" DataField="NomProveedor"/>
        <asp:BoundField HeaderText="Producto" DataField="NomProducto"/>
        <asp:BoundField HeaderText="Descripcion" DataField="Descripcion"/>
        <asp:BoundField HeaderText="Existencias" DataField="Existencias"/>
        <asp:BoundField HeaderText="Precio de Venta" DataField="PrecioVenta"/>
        <asp:BoundField HeaderText="Precio de Compra" DataField="PrecioCompra"/>

    </Columns>
    </asp:GridView>
    
asked by Medina Nualart Martin 05.05.2017 в 18:15
source

2 answers

1

Thank you very much for your response.

already solve it in the following way

<asp:GridView ID="GridView1" runat="server" EmptyDataText="Termino no encontrado" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" AutoGenerateColumns="False" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Vertical">
        <AlternatingRowStyle BackColor="White" />
    <Columns>
        <asp:TemplateField HeaderText="Seleccionar">
            <ItemTemplate>
                <asp:CheckBox Visible='<%# Convert.ToInt32(Eval("Existencias")) > 0 %>' ID="chk" runat="server" OnCheckedChanged="chk_CheckedChanged" AutoPostBack="true" />
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Cantidad">
            <ItemTemplate>
                <asp:DropDownList ID="ddlCantidad1" Enabled='<%# Convert.ToInt32(Eval("Existencias")) > 0 %>' runat="server"></asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:BoundField HeaderText="Codigo" DataField="IdProducto"/>
        <asp:BoundField HeaderText="Marca" DataField="NomMarca"/>
        <asp:BoundField HeaderText="Ciudad" DataField="NomCiudad"/>
        <asp:BoundField HeaderText="Tamaño" DataField="Tamano"/>
        <asp:BoundField HeaderText="Fragilidad" DataField="NomFragilidad"/>
        <asp:BoundField HeaderText="Ubicacion" DataField="NomUbicacion"/>
        <asp:BoundField HeaderText="Proveedor" DataField="NomProveedor"/>
        <asp:BoundField HeaderText="Producto" DataField="NomProducto"/>
        <asp:BoundField HeaderText="Descripcion" DataField="Descripcion"/>
        <asp:BoundField HeaderText="Existencias" DataField="Existencias"/>
        <asp:BoundField HeaderText="Precio de Venta" DataField="PrecioVenta"/>
        <asp:BoundField HeaderText="Precio de Compra" DataField="PrecioCompra"/>
    </Columns>
        <FooterStyle BackColor="#CCCC99" />
        <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
        <RowStyle BackColor="#F7F7DE" />
        <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#FBFBF2" />
        <SortedAscendingHeaderStyle BackColor="#848384" />
        <SortedDescendingCellStyle BackColor="#EAEAD3" />
        <SortedDescendingHeaderStyle BackColor="#575357" />
    </asp:GridView>


foreach (GridViewRow gv1row in GridView1.Rows)
        {
            int existenciasValue = int.Parse(gv1row.Cells[11].Text);
            for (int i = 0; i <= existenciasValue; i++)
            {
                int ix = gv1row.RowIndex;
                DropDownList DdlCantidad = GridView1.Rows[ix].FindControl("DdlCantidad1") as DropDownList;
                //llenar dropdownlist aqui
                DdlCantidad.Items.Add(i.ToString());
            }
        }
    
answered by 13.05.2017 в 04:10
0

A little late, but I found your question and I started to solve it and here it is:

Before proceeding:

  • The DropDownList must be loaded by a function that returns the values and receives a parameter (which is the maximum number of elements it will have).
  • In my example, I load the GridView and the DropDownList using ObjectDataSource controls.

This is the GridView control on the client side:

<asp:GridView ID="gvPrueba" runat="server" AutoGenerateColumns="false" DataSourceID="ODS_gvPrueba" OnRowDataBound="gvPrueba_RowDataBound">
    <Columns>
        <asp:BoundField HeaderText="ID" DataField="id_existencias"/>

        <asp:TemplateField HeaderText="Existencias">
            <ItemTemplate>
                <asp:TextBox ID="txtExistencias" runat="server" Text='<%# Eval("existencias") %>'></asp:TextBox>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Combo existencias">
            <ItemTemplate>
                //<%-- DropDownList que es cargado a través de un control ObjectDataSource --%>
                <asp:DropDownList ID="ddlExistencias" runat="server"     
                    DataTextField="existencias_dll" 
                    DataValueField="id_existencias_dll" DataSourceID="ODS_ddlExistencias">
                </asp:DropDownList>

                //<%-- Á través de un control HiddenField se pasará el valor de existencias a la función que carga nuestro DDL --%>
                <asp:HiddenField ID="hfExistencias" runat="server" Value='<%# Eval("existencias") %>'/>

                //<%-- Control ObjectDataSource que carga el control DropDownList "ddlExistencias" dentro del GridView --%>
                <asp:ObjectDataSource ID="ODS_ddlExistencias" runat="server" SelectMethod="cargar_dllExistencias" TypeName="Solución___GridView_DDL.Existencias" OnSelecting="ODS_ddlExistencias_Selecting">
                    <SelectParameters>
                        <asp:ControlParameter ControlID="hfExistencias" Name="existencias" Type="String" PropertyName="Value"/>
                    </SelectParameters>
                </asp:ObjectDataSource>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

In the server side code part:

public partial class WebForm1 : System.Web.UI.Page
{
    //Variable que será accesible por los métodos; 
    //De esta forma podremos pasar un valor de un método a otro.
    private String _numero;

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    //Método OnRowDataBound del control GridView
    protected void gvPrueba_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow) {
            DropDownList ddl = (DropDownList)e.Row.FindControl("ddlExistencias");
            String valor_hf = ((HiddenField)e.Row.FindControl("hfExistencias")).Value;

            _numero = valor_hf;
            ddl.DataBind();
        }
    }

    //Método OnSelecting del control ObjectDataSource que es el que carga los datos del DropDownList dentro del GridView
    protected void ODS_ddlExistencias_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
        e.InputParameters["existencias"] = _numero;
    }
}

In my example, I use these functions that load the GridView and DropDownList controls respectively.

GridView

public DataTable cargar_gvPrueba() {
    DataTable dt = new DataTable();
    dt.Columns.Add("id_existencias");
    dt.Columns.Add("existencias");

    for (int i = 0; i < 15; i++){
        var dr = dt.NewRow();
        dr["id_existencias"] = i;
        dr["existencias"] = i * 5;

        dt.Rows.Add(dr);
    }
    return dt;
}

DropDownList

public DataTable cargar_dllExistencias(String existencias) {
    if (existencias == "") { existencias = "0"; }

    DataTable dt = new DataTable();
    dt.Columns.Add("id_existencias_dll");
    dt.Columns.Add("existencias_dll");

    int exist = Convert.ToInt32(existencias);
    for (int i = 0; i <= exist; i++) {
        var dr = dt.NewRow();

        dr["id_existencias_dll"] = i;
        dr["existencias_dll"] = i;

        dt.Rows.Add(dr);
    }

    return dt;
}

I pasted you pure code, I know, but I also took the liberty of making a video explaining all this, which you can see in the following link .

    
answered by 13.05.2017 в 03:27