Calculating price in gridview

I am trying to take two values from my gridview and multiply them together to get the extended price on an item.

I have search all around google and the only things i can find are totaling in the Footer.

so basically i want to do this.

unit_price * qty = extended_price

the qty selection is a dropdown box in the gridview.
and i am trying to get the extended_price to display in a asp:Lable

i would like to use the code behind page for this if possible.

I am using Visual Studio Express and i am on 3.5 framework

Thank You
LVL 6
phoenixfire425Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

muhammadyasirCommented:
Be sure that you are only binding your at once in PageLoad with !Page.IsPostback method and try to specify the index of rows and cells when accessing it inside the Grid.. so try

DropDownList ddl = (DropDownList)GridView1.Rows[e.RowIndex].Cells[0].FindControl("DropDownList1");
string val = ddl.SelectedValue;

0
HainKurtSr. System AnalystCommented:
on select box use autopostback to true
and write a piece of code on the  GridView_RowCommand
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
Dim row As GridViewRow = ContactsGridView.Rows(index)
dim lbl as Label = row.FindControl("lblExtendedPrice")
lb.Text = Cint(row.cells(1).text) *  Cdbl(row.cells(1).text)

Open in new window

0
phoenixfire425Author Commented:
I am not sure i understand what you are tell me to do with the PageLoad.

I am using VB

I translated the C code you provided to

Dim ddl As DropDownList = CType(GridView1.Rows(e.RowIndex).Cells(0).FindControl("DropDownList1"), DropDownList)
Dim val As String =  ddl.SelectedValue

and its stating that its not correct.
"BC30456: 'RowIndex' is not a member of 'System.Web.UI.WebControls.GridViewRowEventArgs'."

Attached is the GridView Code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataSourceID="SqlDataSource1" BackColor="White" BorderColor="#DEDFDE" 
        BorderStyle="None" BorderWidth="1px" CellPadding="4" 
        ForeColor="Black" GridLines="Vertical" CssClass="style1" 
        DataKeyNames="item_id" DataMember="DefaultView" ShowFooter="True">
            <RowStyle BackColor="#F7F7DE" Font-Size="8pt" />
            <Columns>
                <asp:BoundField DataField="item_id" HeaderText="Item ID" 
                    SortExpression="item_id" />
                <asp:BoundField DataField="unit_of_measure" HeaderText="Unit of Measure" 
                    SortExpression="unit_of_measure" />
                <asp:BoundField DataField="item_desc" HeaderText="Item Description" 
                    SortExpression="item_desc" />
                <asp:BoundField DataField="Last Order Date" DataFormatString="{0:d}" 
                    HeaderText="Last Invoice Date" SortExpression="Last Order Date" />
                <asp:TemplateField HeaderText="Price" SortExpression="unit_price">
                    <ItemTemplate>
                        <asp:Label ID="Price" runat="server" Text='<%# Bind("unit_price", "{0:c}") %>'></asp:Label>
                  <div style="visibility: hidden"><%#GetUnitPrice(Decimal.Parse(Eval("unit_price").ToString())).ToString("N2")%></div> 

                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("unit_price") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <FooterTemplate>
                       <%#GetTotal().ToString("N2")%>
                    </FooterTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Qty">
                    <ItemTemplate>
                        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="False" 
                            >
                        <asp:ListItem Text="" Value="" Selected="True"></asp:ListItem>
    <asp:ListItem Text="1" Value="1"></asp:ListItem>
    <asp:ListItem Text="2" Value="2"></asp:ListItem>
    <asp:ListItem Text="3" Value="3"></asp:ListItem>
    <asp:ListItem Text="4" Value="4"></asp:ListItem>
    <asp:ListItem Text="5" Value="5"></asp:ListItem>
    <asp:ListItem Text="6" Value="6"></asp:ListItem>
    <asp:ListItem Text="7" Value="7"></asp:ListItem>
    <asp:ListItem Text="8" Value="8"></asp:ListItem>
    <asp:ListItem Text="9" Value="9"></asp:ListItem>
    <asp:ListItem Text="10" Value="10"></asp:ListItem>
    <asp:ListItem Text="11" Value="11"></asp:ListItem>
    <asp:ListItem Text="12" Value="12"></asp:ListItem>
    <asp:ListItem Text="13" Value="13"></asp:ListItem>
    <asp:ListItem Text="14" Value="14"></asp:ListItem>
    <asp:ListItem Text="15" Value="15"></asp:ListItem>
    <asp:ListItem Text="16" Value="16"></asp:ListItem>
    <asp:ListItem Text="17" Value="17"></asp:ListItem>
    <asp:ListItem Text="18" Value="18"></asp:ListItem>
    <asp:ListItem Text="19" Value="19"></asp:ListItem>
    <asp:ListItem Text="20" Value="20"></asp:ListItem>
    <asp:ListItem Text="21" Value="21"></asp:ListItem>
    <asp:ListItem Text="22" Value="22"></asp:ListItem>
    <asp:ListItem Text="23" Value="23"></asp:ListItem>
    <asp:ListItem Text="24" Value="24"></asp:ListItem>
    <asp:ListItem Text="25" Value="25"></asp:ListItem>
    <asp:ListItem Text="26" Value="26"></asp:ListItem>
    <asp:ListItem Text="27" Value="27"></asp:ListItem>
    <asp:ListItem Text="28" Value="28"></asp:ListItem>
    <asp:ListItem Text="29" Value="29"></asp:ListItem>
    <asp:ListItem Text="30" Value="30"></asp:ListItem>
    <asp:ListItem Text="33" Value="31"></asp:ListItem>
    <asp:ListItem Text="33" Value="32"></asp:ListItem>
    <asp:ListItem Text="33" Value="33"></asp:ListItem>
    <asp:ListItem Text="34" Value="34"></asp:ListItem>
    <asp:ListItem Text="35" Value="35"></asp:ListItem>
    <asp:ListItem Text="36" Value="36"></asp:ListItem>
    <asp:ListItem Text="37" Value="37"></asp:ListItem>
    <asp:ListItem Text="38" Value="38"></asp:ListItem>
    <asp:ListItem Text="39" Value="39"></asp:ListItem>
    <asp:ListItem Text="40" Value="40"></asp:ListItem>
    <asp:ListItem Text="44" Value="41"></asp:ListItem>
    <asp:ListItem Text="44" Value="42"></asp:ListItem>
    <asp:ListItem Text="44" Value="44"></asp:ListItem>
    <asp:ListItem Text="44" Value="45"></asp:ListItem>
    <asp:ListItem Text="45" Value="45"></asp:ListItem>
    <asp:ListItem Text="46" Value="46"></asp:ListItem>
    <asp:ListItem Text="47" Value="47"></asp:ListItem>
    <asp:ListItem Text="48" Value="48"></asp:ListItem>
    <asp:ListItem Text="49" Value="49"></asp:ListItem>
    <asp:ListItem Text="50" Value="50"></asp:ListItem>

                        </asp:DropDownList>
                        <asp:Label runat="server" ID="Qty"></asp:Label>
                    </ItemTemplate>
                    <ItemStyle Font-Bold="True" Font-Size="Medium" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="txtEstimatedTotal">
                    <ItemTemplate>
                       <asp:Label runat="server" ID="Totals"></asp:Label>
                        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    </ItemTemplate>
                    <FooterTemplate>

                    </FooterTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#CCCC99" />
            <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
            <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" 
                Font-Size="XX-Small" />
            <AlternatingRowStyle BackColor="White" Font-Size="XX-Small" />
    </asp:GridView>

Open in new window

0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

HainKurtSr. System AnalystCommented:
try this on GridView_RowCommand
Dim index As Integer = Convert.ToInt32(e.CommandArgument) 
Dim row As GridViewRow = ContactsGridView.Rows(index) 
dim lbl as Label = row.FindControl("lblExtendedPrice") 
dim txtPrice as TextBox = row.FindControl("TextBox1").Text
dim lbQty as Listbox = row.FindControl("DropDownList1")
dim qty as int = Cint(lbQty.SelectedValue)
dim price as double =  Cdbl(txtPrice.Text)
lb.Text = qty * price

Open in new window

0
phoenixfire425Author Commented:
This is the error i received.

Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Web.UI.WebControls.DropDownList' to type 'System.Web.UI.WebControls.ListBox'.

Source Error:


Line 131:        Dim lbl As Label = row.FindControl("Totals")
Line 132:        Dim txtPrice As Label = row.FindControl("Price")
Line 133:        Dim lbQty As ListBox = row.FindControl("DropDownList1")
Line 134:        Dim qty As String = CInt(lbQty.SelectedValue)
Line 135:        Dim price As Double = CDbl(txtPrice.Text)
 
0
HainKurtSr. System AnalystCommented:
Dim lbQty As ListBox = row.FindControl("DropDownList1")
-->
Dim lbQty As DropDownList = row.FindControl("DropDownList1")
0
phoenixfire425Author Commented:
ok that seems to work for totaling. but is there a way to have that fire when he qty is selected from the dropdown list rather then having to press the Select button?
0
HainKurtSr. System AnalystCommented:
try

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="False">
-->
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
0
phoenixfire425Author Commented:
I get the postback but it does not send the trigger to execute the GridView1_RowCommand.

Is there a way to make this a trigger for the postback of the Dropdown list?
0
HainKurtSr. System AnalystCommented:
one way is use a custom dropdownlist

http://www.northwestbusinessintelligence.co.uk/technical/tips/100001-aspnet-gridview-rowcommand-event-from-dropdownlist

the other option is using a button next to the select box, setting style="display:none"
and call click event of this button on client side. look at the link firs, if it is something difficult, then we may go with this option...
0
phoenixfire425Author Commented:
I cannot seem to get that to work.

I put the command name in the field. and when it does the postback northing happens.
0
HainKurtSr. System AnalystCommented:
<style>
.hidden{display:none}
</style>
...
<asp:Button ID="btnGo" runat="server" Text="Go" CommandName="Go" cssClass="hidden"/>

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="False">
...
then loop all dropdownlist and set onChange event

foreach (row as GridViewRow in gv1.Rows)  
  Dim ddlQty As ListBox = row.FindControl("DropDownList1")
  ddl.Attributes.Add("onChange", "submitQyt(this)");  
Next
...
and submitQyt() will be a javascript function
<script>
function submitQyt(dd){
  dd.ParentNode.getElementsByTagName("BUTTON")[0].click();
}
<script>

hope it is clear and works ;)
0
phoenixfire425Author Commented:
I cannot seem to get this to work..  i guess i dont understand this very well on howto set that up.
0
HainKurtSr. System AnalystCommented:
ok, found another way, if above is too complicated to implement ;)

http://asimsajjad.blogspot.com/2009/09/raising-dropdownlist.html

idea is set autopostback to true, and on selectedIndexChanged of dropdown, you get the gridrow... when you find the row, you can use the code you used before...

try this...
ddlDropDownList_SelectedIndexChanged(object sender, EventArgs e)
----------------------------------------------------------------
dim ddlCurrent as DropDownList = sender
dim grdRow as GridViewRow  = ddlCurrentDropDownList.Parent.Parent

dim lbl as Label = row.FindControl("lblExtendedPrice")  
dim txtPrice as TextBox = row.FindControl("TextBox1").Text 
Dim lbQty As DropDownList = row.FindControl("DropDownList1")
dim qty as int = Cint(lbQty.SelectedValue) 
dim price as double =  Cdbl(txtPrice.Text) 
lb.Text = qty * price

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
phoenixfire425Author Commented:
Well it looks like this might work if i can get this issue resolved!

Compiler Error Message: BC30456: 'CommandArgument' is not a member of 'System.EventArgs'.

Source Error:


Line 136:            Dim ddlCurrent As DropDownList = sender
Line 137:            Dim grdRow As GridViewRow = ddlCurrentDropDownList.Parent.Parent
Line 138:            Dim index As Integer = Convert.ToInt32(e.CommandArgument)  <----- here is the issue
Line 139:            Dim row As GridViewRow = GridView1.Rows(index)
Line 140:            Dim lbl As Label = row.FindControl("Totals")
    Protected Sub ddlDropDownList_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)

        Dim ddlCurrentDropDownList As DropDownList = CType(sender, DropDownList)
        Dim grdrDropDownRow As GridViewRow = (CType(ddlCurrentDropDownList.Parent.Parent, GridViewRow))
        Dim lblCurrentStatus As Label = CType(grdrDropDownRow.FindControl("lblOrderStatus"), Label)
        If Not lblCurrentStatus Is Nothing Then
            lblCurrentStatus.Text = ddlCurrentDropDownList.SelectedItem.Text

            Dim ddlCurrent As DropDownList = sender
            Dim grdRow As GridViewRow = ddlCurrentDropDownList.Parent.Parent
            Dim index As Integer = Convert.ToInt32(e.CommandArgument)
            Dim row As GridViewRow = GridView1.Rows(index)
            Dim lbl As Label = row.FindControl("Totals")
            Dim txtPrice As Label = row.FindControl("Price")
            Dim lbQty As DropDownList = row.FindControl("DropDownList1")
            Dim qty As String = CInt(lbQty.SelectedValue)
            Dim price As Double = CDbl(txtPrice.Text)
            lbl.Text = qty * price
        End If
    End Sub

Open in new window

0
phoenixfire425Author Commented:
Well i was able to get it to work. but i am not sure if it is the correct / best way or not.
    Protected Sub ddlDropDownList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim ddlCurrentDropDownList As DropDownList = CType(sender, DropDownList)
        Dim grdrDropDownRow As GridViewRow = (CType(ddlCurrentDropDownList.Parent.Parent, GridViewRow))
        Dim lblCurrentStatus As Label = CType(grdrDropDownRow.FindControl("lblOrderStatus"), Label)
        If Not lblCurrentStatus Is Nothing Then
            lblCurrentStatus.Text = ddlCurrentDropDownList.SelectedItem.Text

            Dim ddlCurrent As DropDownList = sender

            'try to get the rox index
            Dim grdRow As GridViewRow = ddlCurrentDropDownList.Parent.Parent
            Dim Row As GridViewRow = CType(CType(sender, Control).Parent.Parent,  _
                                    GridViewRow)
            'end

            Dim index As Integer = Row.RowIndex
            Dim lbl As Label = row.FindControl("Totals")
            Dim txtPrice As Label = row.FindControl("Price")
            Dim lbQty As DropDownList = row.FindControl("ddlDropDownList")
            Dim qty As String = CInt(lbQty.SelectedValue)
            Dim price As Double = CDbl(txtPrice.Text)
            lbl.Text = qty * price
        End If
    End Sub

Open in new window

0
phoenixfire425Author Commented:
Lastly I guess before i accept this as a solution.. Is there a way to give a total in the footer for this column??
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.