public DsStockFormulario GetAgenciaSucursalStockActualFormularioDataSet()
        {
            try
            {
                DsStockFormulario ds         = new DsStockFormulario();
                SqlParameter      pAgenciaID = new SqlParameter("@AgenciaID", Utiles.BaseDatos.IntToSql(this.AgenciaID));

                /** Se obtiene la cantidad disponible de formularios para una agencia específica o todas. **/
                Config.Conexion.LlenarTypeDataSet(ds.Datos, System.Data.CommandType.StoredProcedure, "AgenciaSucursalStockActualFormularioALL", pAgenciaID);
                return(ds);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }
        public DsStockFormulario GetStockFormularioByNroFormulario()
        {
            try
            {
                DsStockFormulario ds             = new DsStockFormulario();
                SqlParameter      pNroFormulario = new SqlParameter("@NroFormulario", this.NroInternoDesde);

                /** Se obtiene los datos para un formulario específico. **/
                Config.Conexion.LlenarTypeDataSet(ds.Datos, System.Data.CommandType.StoredProcedure, "StockFormularioByNroFormulario", pNroFormulario);
                return(ds);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }
        public DsStockFormulario GetStockFormularioByEstadoFormularioID(int estadoFormularioID)
        {
            try
            {
                DsStockFormulario ds = new DsStockFormulario();
                SqlParameter      pEstadoFormularioID = new SqlParameter("@EstadoFormularioID", estadoFormularioID);

                /** Se obtiene el rango de formularios en stock según un estado determinado. **/
                Config.Conexion.LlenarTypeDataSet(ds.Datos, System.Data.CommandType.StoredProcedure, "StockFormularioByEstadoFormularioID", pEstadoFormularioID);
                return(ds);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }
        public DsStockFormulario GetStockFormularioByAgencia(DateTime fechaDesde, DateTime fechaHasta)
        {
            try
            {
                DsStockFormulario ds = new DsStockFormulario();

                SqlParameter pFechaDesde = new SqlParameter("@FechaDesde", fechaDesde);
                SqlParameter pFechaHasta = new SqlParameter("@FechaHasta", fechaHasta);
                SqlParameter pAgenciaID  = new SqlParameter("@AgenciaID", Utiles.BaseDatos.IntToSql(this.AgenciaID));

                /** Se obtiene el stock de los formularios según consulta. **/
                Config.Conexion.LlenarTypeDataSet(ds.Datos, System.Data.CommandType.StoredProcedure, "StockFormularioByAgencia", pFechaDesde, pFechaHasta, pAgenciaID);
                return(ds);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }
        private void BindGrid()
        {
            try
            {
                IStockFormulario formularios = StockFormularioFactory.GetStockFormularioFactory();

                #region Cargar grilla dtgRArchivo
                if (this.rbArchivo.Checked && this.rbPorRango.Checked)
                {
                    AdministrarGrillas.Configurar(dtgRArchivo, "StockFormularioID", this.CantidadOpciones);

                    DsStockFormulario ds = formularios.GetStockFormularioByEstadoFormularioID(Convert.ToInt32(NegociosSisPackInterface.SisPack.EstadoFormulario.EnArchivo));

                    this.dtgRArchivo.DataSource = ds;
                    this.dtgRArchivo.DataBind();
                    ds = null;
                    this.dtgRArchivo.Visible = true;
                }
                else
                {
                    this.dtgRArchivo.Visible = false;
                }
                #endregion

                #region Cargar grilla dtgRArchivoFiltro
                if (this.rbArchivo.Checked && this.rbPorDetalle.Checked)
                {
                    AdministrarGrillas.Configurar(dtgRArchivoFiltro, "StockFormularioID", this.CantidadOpciones);

                    DateTime fechaDesdeEnArch = this.txtFechaDesdeEnArch.Text.Trim().Equals("")? DateTime.Parse("01/01/1900") : Utiles.Fechas.FormatFechaDDMMYYYY(this.txtFechaDesdeEnArch.Text.Trim());
                    DateTime fechaHastaEnArch = this.txtFechaHastaEnArch.Text.Trim().Equals("")? DateTime.Parse("01/01/2100") : Utiles.Fechas.FormatFechaDDMMYYYY(this.txtFechaHastaEnArch.Text.Trim());
                    formularios.EstadoStockFormularioID = Convert.ToInt32(NegociosSisPackInterface.SisPack.EstadoStockFormulario.EnArchivo);

                    DsStockFormulario ds = formularios.GetStockFormularioDataSet(fechaDesdeEnArch, fechaHastaEnArch);

                    this.dtgRArchivoFiltro.DataSource = ds;
                    this.dtgRArchivoFiltro.DataBind();
                    ds = null;
                    this.dtgRArchivoFiltro.Visible = true;
                }
                else
                {
                    this.dtgRArchivoFiltro.Visible = false;
                }
                #endregion

                #region Cargar grilla hgRAgenciaFiltro
                if (this.rbAgencias.Checked && this.rbPorFiltros.Checked)
                {
                    DataSet ds = new DataSet();
                    SisPackController.AdministrarGrillas.Configurar(this.hgRAgenciaFiltro, "AgenciaID", this.CantidadOpciones);

                    /* Cargar el primer nivel. */
                    formularios.AgenciaID = this.busqAgencia.AgenciaID.Trim().Equals("")? 0:Convert.ToInt32(this.busqAgencia.AgenciaID.Trim());
                    DsStockFormulario dsAgencias = formularios.GetAgenciaSucursalStockActualFormularioDataSet();

                    ds.Tables.Add(dsAgencias.Datos.Clone());
                    ds.Tables[0].TableName = "Agencias";

                    foreach (System.Data.DataRow row in dsAgencias.Datos.Rows)
                    {
                        ds.Tables["Agencias"].ImportRow(row);
                    }

                    /* Cargar el segundo nivel */
                    DateTime          fechaDesde = this.txtFechaDesde.Text.Trim().Equals("")? DateTime.Parse("01/01/1900") : Utiles.Fechas.FormatFechaDDMMYYYY(this.txtFechaDesde.Text.Trim());
                    DateTime          fechaHasta = this.txtFechaHasta.Text.Trim().Equals("")? DateTime.Parse("01/01/2100") : Utiles.Fechas.FormatFechaDDMMYYYY(this.txtFechaHasta.Text.Trim());
                    DsStockFormulario dsFormulariosPorAgencia = formularios.GetStockFormularioByAgencia(fechaDesde, fechaHasta);

                    ds.Tables.Add(dsFormulariosPorAgencia.Datos.Clone());
                    ds.Tables[1].TableName = "FormulariosAgencia";

                    foreach (System.Data.DataRow row in dsFormulariosPorAgencia.Datos.Rows)
                    {
                        ds.Tables["FormulariosAgencia"].ImportRow(row);
                    }

                    /* Setear la relación para asociar niveles */
                    DataColumn dc1 = ds.Tables[0].Columns["AgenciaID"];
                    DataColumn dc2 = ds.Tables[1].Columns["AgenciaID"];

                    DataRelation dr = new DataRelation("AgenciaID_FormulariosAgencia", dc1, dc2, false);
                    ds.Relations.Add(dr);

                    this.hgRAgenciaFiltro.DataSource = ds;
                    this.hgRAgenciaFiltro.DataMember = "Agencias";
                    this.hgRAgenciaFiltro.DataBind();
                    this.hgRAgenciaFiltro.Visible = true;
                }
                else
                {
                    this.hgRAgenciaFiltro.Visible = false;
                }
                #endregion

                #region Cargar grilla dtgRFormulario
                if (this.rbAgencias.Checked && this.rbPorNroFormulario.Checked)
                {
                    AdministrarGrillas.Configurar(dtgRFormulario, "StockFormularioID", this.CantidadOpciones);

                    formularios.NroInternoDesde = this.txtNroFormulario.Text.Trim().Equals("")?0:Convert.ToInt32(this.txtNroFormulario.Text.Trim());
                    DsStockFormulario ds = formularios.GetStockFormularioByNroFormulario();

                    this.dtgRFormulario.DataSource = ds;
                    this.dtgRFormulario.DataBind();
                    ds = null;
                    this.dtgRFormulario.Visible = true;
                }
                else
                {
                    this.dtgRFormulario.Visible = false;
                }
                #endregion

                formularios = null;
            }
            catch (Exception ex)
            {
                ((ErrorWeb)phErrores.Controls[0]).setMensaje(ex.Message);
            }
        }