April 30, 2012

Gridview Row Databound / Calculate Total in Gridview


code:
                    <asp:GridView ID="grdClients" runat="server" AutoGenerateColumns="false" CssClass="mGrid"
                        AlternatingRowStyle-CssClass="alt" OnRowDataBound="grdClients_RowDataBound" ShowFooter="True">
                        <AlternatingRowStyle CssClass="alt"></AlternatingRowStyle>
                        <Columns>
                            <asp:BoundField DataField="Name" HeaderText="Clients" />
                            <asp:BoundField DataField="Family" HeaderText="Code" Visible="false" />
                            <asp:BoundField DataField="Long_Name" HeaderText="Client Name" Visible="false" />
                            <asp:BoundField DataField="Brokerage" ItemStyle-HorizontalAlign="Right" HeaderText="Rev"
                                DataFormatString="{0:N}">
                                <ItemStyle HorizontalAlign="Right"></ItemStyle>
                            </asp:BoundField>
                            <asp:BoundField DataField="SharePercent" HeaderText="Share in Revenue %" ItemStyle-Width="20px"
                                ItemStyle-HorizontalAlign="Right" DataFormatString="{0:N}">
                                <ItemStyle HorizontalAlign="Right" Width="20px"></ItemStyle>
                            </asp:BoundField>
                        </Columns>
                        <FooterStyle Font-Bold="True" />
                        <HeaderStyle Font-Size="11px" />
                    </asp:GridView>



 protected void grdClients_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            decimal TotalExps = (decimal)DataBinder.Eval(e.Row.DataItem, "Brokerage");
            _TotalExpTotal += TotalExps;
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            //Label lblSummary = (Label)e.Row.FindControl("lblSummary");
            //lblSummary.Text = String.Format("{0:D}", _TotalExpTotal);
            e.Row.Cells[0].Text = "Total";
            e.Row.Cells[0].HorizontalAlign = HorizontalAlign.Center;

            e.Row.Cells[3].Text = String.Format("{0:N}", _TotalExpTotal);

        }

    }

Last and First Day of the previous months


--LAST DATE OF PREVIOUS MONTH
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0))

--GET FIRST DATE OF PREVIOUS 3 MONTHS
SELECT @STARTDATE = DATEADD(DD,-(DAY(DATEADD(MM,1, GETDATE()))-1),DATEADD(MM,- 3, GETDATE()))