Losing one row of datagrid when changing dropdownlist inside it.

  • Thread starter Luis Esteban Valencia
  • Start date
L

Luis Esteban Valencia

Hello. I have a datagrid with one row. I have a button that adds a new row.
I am trying to implement that when the user selects one product it must
change the price on the quantity column. Anyway when the user selects the
first dropdownlist the dropdownselected_indexchanged is firring but when the
user chagnes the second dropdownlist its not firing.

aahh! if the user selects any of both dropdownlist the datagrid stays with
only one row?

Whjy??

<asp:datagrid id="dgpedidos" runat="server" Width="100%"
AutoGenerateColumns="False">
<ItemStyle CssClass="registros"></ItemStyle>
<HeaderStyle CssClass="titulostablas"></HeaderStyle>
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton runat="server" Text="Eliminar"
CommandName="Delete" CausesValidation="false"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Producto">
<ItemTemplate>
<asp:DropDownList id="ddlproductos" runat="server"
AutoPostBack="True"
OnSelectedIndexChanged="DropDown_SelectedIndexChanged"></asp:DropDownList>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton id="LinkButton1" runat="server"
CommandName="agregarproducto">Agregar Producto</asp:LinkButton>
</FooterTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Cantidad">
<ItemTemplate>
<asp:TextBox id="txtcantidad" runat="server" Width="44px"
CssClass="textos" AutoPostBack="True"
OnTextChanged="calcularsubtotal"></asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Precio">
<ItemTemplate>
<asp:TextBox id=txtprecio runat="server" Width="60px"
CssClass="textos" Text='<%# DataBinder.Eval(Container,
"DataItem.precioespecifico") %>'>
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Iva">
<ItemTemplate>
<asp:TextBox id=txtivaporproducto runat="server" Width="36px"
CssClass="textos" Text='<%# DataBinder.Eval(Container, "DataItem.iva") %>'>
</asp:TextBox><FONT size="2">%</FONT>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Subtotal">
<ItemTemplate>
<asp:TextBox id="txtsubtotalporproducto" runat="server"
Width="68px" CssClass="textos"></asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>

the function that adds a new row is this.

Private Sub LinkButton2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles LinkButton2.Click
Dim tcells As TableCellCollection

'tcells = e.Item.Cells

Dim lnkeliminarcel As New TableCell
Dim productcell As New TableCell
Dim cantidadcell As New TableCell
Dim preciocell As New TableCell
Dim taxescell As New TableCell
Dim subtotalcell As New TableCell

Dim ddlproductos As New DropDownList
ddlproductos.AutoPostBack = True
'ddlproductos = e.Item.Cells(1).FindControl("ddlproductos")
ddlproductos.Attributes.Add("OnSelectedIndexChanged",
"DropDown_SelectedIndexChanged")

ddlproductos.DataSource = CType(Session("productos"), DataSet)
ddlproductos.DataTextField = "descripcion"
ddlproductos.DataValueField = "idproducto"
ddlproductos.DataBind()

productcell.Controls.Add(ddlproductos)


Dim lnkbutton As New LinkButton
lnkbutton.Text = "Eliminar"
lnkbutton.CommandName = "Delete"

lnkeliminarcel.Controls.Add(lnkbutton)


Dim txtcantidad As New TextBox
txtcantidad.Text = 1
txtcantidad.CssClass = "textos"
txtcantidad.Width = Unit.Pixel(44)

cantidadcell.Controls.Add(txtcantidad)

Dim txtprecio As New TextBox
txtprecio.CssClass = "textos"
txtprecio.Width = Unit.Pixel(60)
txtprecio.Text =
objconsultas.precioxproducto(ddlproductos.SelectedValue)
preciocell.Controls.Add(txtprecio)

Dim txtivaporproducto As New TextBox
txtivaporproducto.CssClass = "textos"
txtivaporproducto.Width = Unit.Pixel(36)
txtivaporproducto.Text =
objconsultas.ivaporproducto(ddlproductos.SelectedValue)
taxescell.Controls.Add(txtivaporproducto)

Dim cantidad As Int32 = Convert.ToInt32(txtcantidad.Text)
Dim price As Int32 = Convert.ToInt32(txtprecio.Text)
Dim subtotal As Decimal = cantidad * price - ((cantidad * price) *
(txtivaporproducto.Text / 100))

Dim txtsubtotalporproducto As New TextBox

txtsubtotalporproducto.CssClass = "textos"
txtsubtotalporproducto.Width = Unit.Pixel(68)

txtsubtotalporproducto.Text = subtotal.ToString
subtotalcell.Controls.Add(txtsubtotalporproducto)


Dim di As New DataGridItem(0, 0, ListItemType.Item)
di.Cells.Add(lnkeliminarcel)
di.Cells.Add(productcell)
di.Cells.Add(cantidadcell)
di.Cells.Add(preciocell)
di.Cells.Add(taxescell)
di.Cells.Add(subtotalcell)

dgpedidos.Controls(0).Controls.Add(di)

End Sub

I also put something on the itemdatabound ç

Private Sub dgpedidos_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
dgpedidos.ItemDataBound
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then
Dim ddlproductos As New DropDownList
ddlproductos.AutoPostBack = True
ddlproductos = e.Item.Cells(1).FindControl("ddlproductos")
ddlproductos.Attributes.Add("OnSelectedIndexChanged",
"DropDown_SelectedIndexChanged")

ddlproductos.DataSource = CType(Session("productos"), DataSet)
ddlproductos.DataTextField = "descripcion"
ddlproductos.DataValueField = "idproducto"
ddlproductos.DataBind()


Dim txtquantity As TextBox
txtquantity = e.Item.Cells(2).Controls(1)
txtquantity.Text = 1
Dim txtprice As TextBox
txtprice = e.Item.Cells(3).Controls(1)
Dim txtsubtotal As TextBox
Dim txtiva As TextBox
txtiva = e.Item.Cells(4).Controls(1)
txtsubtotal = e.Item.Cells(5).Controls(1)
txtsubtotal.Text = (Convert.ToInt32(txtquantity.Text) *
Convert.ToInt32(txtprice.Text)) - (Convert.ToInt32(txtquantity.Text) *
Convert.ToInt32(txtprice.Text) * (txtiva.Text / 100))
recorrerdatagridycalcular()
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top