• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

My datagrid addnew row button is only inserting one row?

Please take a loogk at my code.

<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"></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", "{0:N0}%") %>'>
                                                                                          </asp:TextBox>
                                                                                    </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></P>
                                                      </TD>
                                                </TR>
                                                <TR>
                                                      <TD width="50%"></TD>
                                                      <TD width="68" style="WIDTH: 68px"></TD>
                                                      <TD width="15%"></TD>
                                                      <TD width="15%"><STRONG><FONT size="3">Subtotal</FONT></STRONG></TD>
                                                      <TD width="15%"><STRONG><FONT size="3"><asp:textbox id="txtsubtotal" runat="server" Width="80px" CssClass="textos"></asp:textbox></FONT></STRONG></TD>
                                                </TR>
                                                <TR>
                                                      <TD width="50%"><STRONG><FONT size="3">
                                                                        <asp:LinkButton id="LinkButton2" runat="server" CommandName="agregarproducto">Agregar Producto</asp:LinkButton>



 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.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 Int32 = cantidad * price

        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
0
djhex
Asked:
djhex
  • 3
  • 2
1 Solution
 
YZlatCommented:
But that's what it should do - add one new row. What do you want it to do?
0
 
djhexAuthor Commented:
It adds a new row. and then the datagird have 2 rows tight?
When I click again on the linkbutton it DOesnt add a 3rd row.

0
 
laotzi2000Commented:
It seems you get the rows from a dataset stored in a sesson variable
After you insert a row to the datagrid, you should also add a row to that
dataset and store it again in session.
Or next time your datagrid binds to the dataset again, what you have
inserted will be lost since it is not in the dataset.

I think it's easier to only modify the dataset when you want to insert a row
and rebind the datagrid to the dataset.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
djhexAuthor Commented:
Could You help me with the code? How can I achieve that according to my code.

Thanks. If you want more points you will have them.
0
 
laotzi2000Commented:
in your linkbutton insert function:

dim ds as dataset = session("productos")
dim row as datarow = ds.tables(0).newrow
row("column one") = value1
row("column two") = value2
.....
row("column n") = valuen
ds.tables(0).rows.add(row)

session("productos") = ds
datagrid1.datasource = ds
datagrid1.databind

That's all you need, I think.
And it's for demonstration, not the exact code.
0
 
djhexAuthor Commented:
I will give you 500 points more if you tell me exactly how to put it according to my code.
Check aboe the datagrid code if needed.

 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
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now