How to add a row to a datagrid?

  • Thread starter Luis Esteban Valencia
  • Start date
L

Luis Esteban Valencia

Please check my datagrid and code behind code and help me if possible.
I am making asingle sales system and I need to add a new row with a
linkbutton that I put on the footer of the datagrid.

My datagrid has a product column (dropdownlist), quanitity( the user can
enter text here), price (not editable), taxes (not editable) and
subtotal, Price * quantity



<asp:datagrid id="dgpedidos" runat="server" Width="100%"
ShowFooter="True" AutoGenerateColumns="False">


<ItemStyle CssClass="registros"></ItemStyle>


<HeaderStyle CssClass="titulostablas"></HeaderStyle>


<Columns>


<asp:ButtonColumn Text="Eliminar"
CommandName="Delete"></asp:ButtonColumn>


<asp:TemplateColumn HeaderText="Producto">


<ItemTemplate>


<asp:DropDownList id="ddlproductos" runat="server"></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"></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>



This is my code behind if needed



Dim objconsultas As New LBDatos.consultas



Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

If Not Page.IsPostBack Then

cargarproductos()

cargardatagrid()

End If

End Sub



Public Sub cargarproductos()

Session("productos") =
objconsultas.todosproductoxCia(Session("idcompania"))

End Sub



Private Sub cargardatagrid()

Dim ds As DataSet

Dim dc1 As New DataColumn("SubTotal",
System.Type.GetType("System.Decimal")) 'i am assuming your stored proc
does not contain a column SubTotal

Dim dc2 As New DataColumn("Cantidad",
System.Type.GetType("System.Int32"))

ds = objconsultas.productoxCia(Session("idcompania"))

ds.Tables(0).Columns.Add(dc2)

dc1.Expression = "Cantidad * Precioespecifico"

ds.Tables(0).Columns.Add(dc1)

dgpedidos.DataSource = ds

dgpedidos.DataBind()



End Sub









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.DataSource = CType(Session("productos"),
DataSet)

ddlproductos.DataTextField = "descripcion"

ddlproductos.DataValueField = "idproducto"

ddlproductos.DataBind()

End If

End Sub







Private Sub dgpedidos_ItemCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
dgpedidos.ItemCommand

If e.CommandName = "agregarproductos" Then

Dim di As DataGridItem

di.





End If

End Sub











Thanks in advance
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,076
Latest member
OrderKetoBeez

Latest Threads

Top