Calculated field in GridView

Last post 10-10-2008 4:34 AM by jordan3114. 5 replies.

Sort Posts:

  • Calculated field in GridView

    10-08-2008, 5:32 AM
    • Member
      17 point Member
    • jordan3114
    • Member since 07-15-2007, 5:04 AM
    • Posts 120

     i have 3 field in gridview called qty, unitPrice, subAmount

    in this gridview it can have many row so if i use query the subAmount like select qty * unitPrice as subAmount from price .. it only worked with 1 row...

    now i wan it to auto calculated the subAmount when i modify qty or subAmount based on that selected row.

    in rowupdating method how can i do it in there ?

    any help would be appreciated .

    Thanks

     

  • Re: Calculated field in GridView

    10-08-2008, 9:02 AM
    • Participant
      1,088 point Participant
    • joseabie
    • Member since 04-07-2008, 4:56 AM
    • Abu Dhabi
    • Posts 299
  • Re: Calculated field in GridView

    10-08-2008, 10:39 AM
    • Member
      17 point Member
    • jordan3114
    • Member since 07-15-2007, 5:04 AM
    • Posts 120

     It other way to do it except using object data source ?

    Thanks

  • Re: Calculated field in GridView

    10-10-2008, 3:12 AM
    Answer

    HI jordan3114

    See my sample :

     

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox qty =  this.GridView1.Rows[e.RowIndex].FindControl("TextBox1") as TextBox;
            TextBox unitprice = this.GridView1.Rows[e.RowIndex].FindControl("TextBox2") as TextBox;
            e.NewValues["subamount"] = Convert.ToInt32(qty.Text) * Convert.ToInt32(unitprice.Text);
    
        }
      
       <form id="form1" runat="server">
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString2 %>"
                DeleteCommand="DELETE FROM [qty] WHERE [id] = @id" InsertCommand="INSERT INTO [qty] ([id], [qty], [unitprice], [subamount]) VALUES (@id, @qty, @unitprice, @subamount)"
                SelectCommand="SELECT * FROM [qty]" UpdateCommand="UPDATE [qty] SET [qty] = @qty, [unitprice] = @unitprice, [subamount] = @subamount WHERE [id] = @id">
                <DeleteParameters>
                    <asp:Parameter Name="id" Type="Int64" />
                </DeleteParameters>
                <UpdateParameters>
                    <asp:Parameter Name="qty" Type="Int64" />
                    <asp:Parameter Name="unitprice" Type="Int64" />
                    <asp:Parameter Name="subamount" Type="Int64" />
                    <asp:Parameter Name="id" Type="Int64" />
                </UpdateParameters>
                <InsertParameters>
                    <asp:Parameter Name="id" Type="Int64" />
                    <asp:Parameter Name="qty" Type="Int64" />
                    <asp:Parameter Name="unitprice" Type="Int64" />
                    <asp:Parameter Name="subamount" Type="Int64" />
                </InsertParameters>
            </asp:SqlDataSource>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id"
                DataSourceID="SqlDataSource1" OnRowUpdating="GridView1_RowUpdating">
                <Columns>
                    <asp:CommandField ShowEditButton="True" />
                    <asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" SortExpression="id" />
                    <asp:TemplateField HeaderText="qty" SortExpression="qty">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("qty") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Bind("qty") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="unitprice" SortExpression="unitprice">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("unitprice") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label2" runat="server" Text='<%# Bind("unitprice") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </form>

     

     


    Samu Zhang
    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
  • Re: Calculated field in GridView

    10-10-2008, 4:07 AM
    • Member
      17 point Member
    • jordan3114
    • Member since 07-15-2007, 5:04 AM
    • Posts 120
    Thanks Samu,  it worked like a charm Big Smile
  • Re: Calculated field in GridView

    10-10-2008, 4:34 AM
    • Member
      17 point Member
    • jordan3114
    • Member since 07-15-2007, 5:04 AM
    • Posts 120

     sorry got another question again ^^

    let say i got 2 gridview called gridview1 and gridview2

    when i updated the data in the gridview1 then at the same time i also need to update the data in gridview2.

    protected void gridview1_RowUpdating(){

    is it possible to update those value to another gridview .

    like e.NewValues["subTotal"] to gridview2

    //


    }

     

    please advise,  thanks

Page 1 of 1 (6 items)