How to change row data when dropdownlist changes?

  • Thread starter Luis Esteban Valencia
  • Start date
L

Luis Esteban Valencia

Hello I have a datagrid with a dropdownlist that has the products,
another column has the price of the product and when the user changes
the product it also must change the price how can I achieve that.



Thanks

Datagrid html



<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>



And my codebehind



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
 
J

Jignesh Desai

Hi Luis,

You are on the correct path... make Autopostback property of dropdown to true, when selecttion changes in dropdown it will trigger
ItemCommand event.
in item commend event based on selection retreive more infomation about the product like price and u can assign it to textbox

HTH
Regards,
Jignesh Desai.
www.dotnetjini.com


Hello I have a datagrid with a dropdownlist that has the products, another column has the price of the product and when the user changes the product it also must change the price how can I achieve that.



Thanks

Datagrid html



<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>



And my codebehind



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
 
L

Luis Esteban Valencia

How would it be in code?
"Jignesh Desai" <[email protected]> escribió en el mensaje Hi Luis,

You are on the correct path... make Autopostback property of dropdown to true, when selecttion changes in dropdown it will trigger
ItemCommand event.
in item commend event based on selection retreive more infomation about the product like price and u can assign it to textbox

HTH
Regards,
Jignesh Desai.
www.dotnetjini.com


Hello I have a datagrid with a dropdownlist that has the products, another column has the price of the product and when the user changes the product it also must change the price how can I achieve that.



Thanks

Datagrid html



<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>



And my codebehind



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
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top