[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Calculating price in gridview

Posted on 2010-03-24
17
Medium Priority
?
457 Views
Last Modified: 2013-11-26
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
0
Comment
Question by:phoenixfire425
  • 9
  • 7
17 Comments
 
LVL 5

Expert Comment

by:muhammadyasir
ID: 28462239
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
 
LVL 61

Expert Comment

by:HainKurt
ID: 28462787
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
 
LVL 6

Author Comment

by:phoenixfire425
ID: 28462973
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 61

Expert Comment

by:HainKurt
ID: 28463989
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
 
LVL 6

Author Comment

by:phoenixfire425
ID: 28469050
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
 
LVL 61

Expert Comment

by:HainKurt
ID: 28470035
Dim lbQty As ListBox = row.FindControl("DropDownList1")
-->
Dim lbQty As DropDownList = row.FindControl("DropDownList1")
0
 
LVL 6

Author Comment

by:phoenixfire425
ID: 28471474
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
 
LVL 61

Expert Comment

by:HainKurt
ID: 28473229
try

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="False">
-->
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">
0
 
LVL 6

Author Comment

by:phoenixfire425
ID: 28474681
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
 
LVL 61

Expert Comment

by:HainKurt
ID: 28477180
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
 
LVL 6

Author Comment

by:phoenixfire425
ID: 28479458
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
 
LVL 61

Expert Comment

by:HainKurt
ID: 28479683
<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
 
LVL 6

Author Comment

by:phoenixfire425
ID: 28557182
I cannot seem to get this to work..  i guess i dont understand this very well on howto set that up.
0
 
LVL 61

Accepted Solution

by:
HainKurt earned 500 total points
ID: 28632933
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
 
LVL 6

Author Comment

by:phoenixfire425
ID: 28695487
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
 
LVL 6

Author Comment

by:phoenixfire425
ID: 28700053
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
 
LVL 6

Author Comment

by:phoenixfire425
ID: 28976560
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Is your organization moving toward a cloud and mobile-first environment? In this transition, your IT department will encounter many challenges, such as navigating how to: Deploy new applications and services to a growing team Accommodate employee…
Suggested Courses

591 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