Monday, 28 May 2012

Subtotal in Gridview control

Subtotal in Gridview control in order Details page on Raasa.


Step 1 :

<asp:GridView ID="gvworderdetails" runat="server"  AutoGenerateColumns="False"
                                onrowdatabound="gvworderdetails_RowDataBound" ShowFooter="True"
                                Width="100%" >
                                <Columns>
                                    <asp:BoundField DataField="type" HeaderText="Type" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" ItemStyle-Height="20px"/>
                                    <asp:BoundField DataField="item_desc" HeaderText="Item Description" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" ItemStyle-Height="20px"/>
                                    <asp:BoundField DataField="status" HeaderText="Status" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" ItemStyle-Height="20px"/>
                                    <%--<asp:BoundField DataField="details" HeaderText="Details" />--%>
                                    <asp:BoundField DataField="price" HeaderText="Price" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center"/>
                                       
                                    <asp:TemplateField HeaderText="Qty">
                                    <FooterStyle Font-Bold="true" Font-Size="13px" />
                                        <FooterTemplate>Grand Total :</FooterTemplate>
                                        <ItemTemplate>
                                            <asp:Label ID="lblqty" runat="server" Text='<%# Eval("qty") %>'></asp:Label>
                                        </ItemTemplate>
                                        <ItemStyle HorizontalAlign="Center" Height="20px"/>
                                        <FooterStyle HorizontalAlign="Right" Height="20px"/>
                                    </asp:TemplateField>
                                    <asp:TemplateField HeaderText="Sub Total">
                                        <ItemTemplate>
                                            <asp:Label ID="lblsubtot" runat="server" Text='<%# Eval("SubTotal") %>'></asp:Label>
                                        </ItemTemplate>
                                        <FooterTemplate>
                                            <asp:Label ID="lbltotal" runat="server" Font-Bold="True" Font-Size="13px"></asp:Label>
                                        </FooterTemplate>
                                        <ItemStyle HorizontalAlign="Center" Height="20px"/>
                                        <FooterStyle HorizontalAlign="Center" Height="20px"/>
                                    </asp:TemplateField>                                      
                                </Columns>
                                <HeaderStyle Height="25px" BackColor="#3b6c01" ForeColor="#ffffff" />
                            </asp:GridView>




Step 2 :



decimal total = 0;
        DataSet ds = new DataSet();
        DataSet ds1 = new DataSet();
        DataSet ds2 = new DataSet();


 public void BindGrid(string order_id)
        {
            //
            bl_raasa csobj = new bl_raasa();
            ds2 = csobj.GetGridBind(order_id);
            if (ds2.Tables[0].Rows.Count > 0)
            {
                gvworderdetails.DataSource = ds2.Tables[0];
                gvworderdetails.DataBind();
            }
        }

        protected void gvworderdetails_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                total += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "SubTotal"));
              
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                Label lbl = (Label)e.Row.FindControl("lblTotal");
                lbl.Text = total.ToString() ;
            }
        }




No comments:

Post a Comment

Post a Comment