protected void GrdRegistration_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        Label lblId = (Label)GrdRegistration.Rows[e.RowIndex].FindControl("lblId");

        try
        {
            Stock stock = StockDB.GetByID(Convert.ToInt32(lblId.Text));
            if (stock != null)
            {
                StockDB.Delete(Convert.ToInt32(lblId.Text));
                StockUpdateHistoryDB.Insert(stock.Organisation.OrganisationID, stock.Offering.OfferingID, -1 * stock.Quantity, false, true, Convert.ToInt32(Session["StaffID"]));
            }
        }
        catch (ForeignKeyConstraintException fkcEx)
        {
            if (Utilities.IsDev())
            {
                HideTableAndSetErrorMessage("Can not delete because other records depend on this : " + fkcEx.Message);
            }
            else
            {
                HideTableAndSetErrorMessage("Can not delete because other records depend on this");
            }
        }

        FillGrid();
        FillGridUpdateHistory();
    }
    protected void GrdRegistration_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        Label        lblId                = (Label)GrdRegistration.Rows[e.RowIndex].FindControl("lblId");
        DropDownList ddlOffering          = (DropDownList)GrdRegistration.Rows[e.RowIndex].FindControl("ddlOffering");
        DropDownList ddlWarningAmount     = (DropDownList)GrdRegistration.Rows[e.RowIndex].FindControl("ddlWarningAmount");
        DropDownList ddlQuantity_Zeros    = (DropDownList)GrdRegistration.Rows[e.RowIndex].FindControl("ddlQuantity_Zeros");
        DropDownList ddlQuantity_Tens     = (DropDownList)GrdRegistration.Rows[e.RowIndex].FindControl("ddlQuantity_Tens");
        DropDownList ddlQuantity_Hundreds = (DropDownList)GrdRegistration.Rows[e.RowIndex].FindControl("ddlQuantity_Hundreds");


        Stock stock = StockDB.GetByID(Convert.ToInt32(lblId.Text));

        if (stock != null)
        {
            int qty = 100 * Convert.ToInt32(ddlQuantity_Hundreds.SelectedValue) + 10 * Convert.ToInt32(ddlQuantity_Tens.SelectedValue) + Convert.ToInt32(ddlQuantity_Zeros.SelectedValue);
            StockDB.Update(stock.StockID, stock.Organisation.OrganisationID, stock.Offering.OfferingID, qty, Convert.ToInt32(ddlWarningAmount.SelectedValue));
            if (qty != stock.Quantity)
            {
                StockUpdateHistoryDB.Insert(stock.Organisation.OrganisationID, stock.Offering.OfferingID, qty - stock.Quantity, false, false, Convert.ToInt32(Session["StaffID"]));
            }
        }

        GrdRegistration.EditIndex = -1;
        FillGrid();
        FillGridUpdateHistory();
    }
    protected void FillGridUpdateHistory()
    {
        Organisation org = null;

        if (IsValidFormID())
        {
            org = OrganisationDB.GetByID(GetFormID());
        }

        DataTable dt = org == null?StockUpdateHistoryDB.GetDataTable() : StockUpdateHistoryDB.GetDataTable_ByOrg(org.OrganisationID);

        Session["registerstockupdatehistory_data"] = dt;

        if (dt.Rows.Count > 0)
        {
            if (IsPostBack && Session["registerstockupdatehistory_sortexpression"] != null && Session["registerstockupdatehistory_sortexpression"].ToString().Length > 0)
            {
                DataView dataView = new DataView(dt);
                dataView.Sort = Session["registerstockupdatehistory_sortexpression"].ToString();
                GrdUpdateHistory.DataSource = dataView;
            }
            else
            {
                GrdUpdateHistory.DataSource = dt;
            }


            try
            {
                GrdUpdateHistory.DataBind();
                GrdUpdateHistory.PagerSettings.FirstPageText = "1";
                GrdUpdateHistory.PagerSettings.LastPageText  = GrdUpdateHistory.PageCount.ToString();
                GrdUpdateHistory.DataBind();
            }
            catch (Exception ex)
            {
                SetErrorMessage("", ex.ToString());
            }
        }
        else
        {
            dt.Rows.Add(dt.NewRow());
            GrdUpdateHistory.DataSource = dt;
            GrdUpdateHistory.DataBind();

            int TotalColumns = GrdUpdateHistory.Rows[0].Cells.Count;
            GrdUpdateHistory.Rows[0].Cells.Clear();
            GrdUpdateHistory.Rows[0].Cells.Add(new TableCell());
            GrdUpdateHistory.Rows[0].Cells[0].ColumnSpan = TotalColumns;
            GrdUpdateHistory.Rows[0].Cells[0].Text       = "No Record Found";
        }
    }
    protected void GrdRegistration_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("Insert"))
        {
            Organisation org = OrganisationDB.GetByID(GetFormID());
            if (org == null)
            {
                HideTableAndSetErrorMessage("");
                return;
            }

            DropDownList ddlOffering          = (DropDownList)GrdRegistration.FooterRow.FindControl("ddlNewOffering");
            DropDownList ddlWarningAmount     = (DropDownList)GrdRegistration.FooterRow.FindControl("ddlNewWarningAmount");
            DropDownList ddlQuantity_Zeros    = (DropDownList)GrdRegistration.FooterRow.FindControl("ddlNewQuantity_Zeros");
            DropDownList ddlQuantity_Tens     = (DropDownList)GrdRegistration.FooterRow.FindControl("ddlNewQuantity_Tens");
            DropDownList ddlQuantity_Hundreds = (DropDownList)GrdRegistration.FooterRow.FindControl("ddlNewQuantity_Hundreds");

            try
            {
                Stock stock = StockDB.GetOfferingByOrgAndOffering(org.OrganisationID, Convert.ToInt32(ddlOffering.SelectedValue));
                if (stock == null)
                {
                    int qty = 100 * Convert.ToInt32(ddlQuantity_Hundreds.SelectedValue) + 10 * Convert.ToInt32(ddlQuantity_Tens.SelectedValue) + Convert.ToInt32(ddlQuantity_Zeros.SelectedValue);
                    StockDB.Insert(org.OrganisationID, Convert.ToInt32(ddlOffering.SelectedValue), qty, Convert.ToInt32(ddlWarningAmount.SelectedValue));
                    StockUpdateHistoryDB.Insert(org.OrganisationID, Convert.ToInt32(ddlOffering.SelectedValue), qty, true, false, Convert.ToInt32(Session["StaffID"]));
                }
            }
            catch (UniqueConstraintException)
            {
                // happens when 2 forms allow adding - do nothing and let form re-update
                ;
            }
            FillGrid();
            FillGridUpdateHistory();
        }
    }