?
Solved

My datagrid addnew row button is only inserting one row?

Posted on 2005-03-23
6
Medium Priority
?
201 Views
Last Modified: 2010-04-07
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
Comment
Question by:djhex
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 13613507
But that's what it should do - add one new row. What do you want it to do?
0
 
LVL 10

Author Comment

by:djhex
ID: 13613645
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
 
LVL 12

Expert Comment

by:laotzi2000
ID: 13614026
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 10

Author Comment

by:djhex
ID: 13614162
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
 
LVL 12

Accepted Solution

by:
laotzi2000 earned 1000 total points
ID: 13614249
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
 
LVL 10

Author Comment

by:djhex
ID: 13614339
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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month7 days, 23 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question