private void LeerEstructuraSegmento(int?Id_Cte, int?Id_Seg)
        {
            try
            {
                DataSet       dsEstructuraSegmento = new DataSet();
                StringBuilder tablahtml            = new StringBuilder();
                DataTable     dtTotalAreas         = new DataTable();
                DataTable     dtTotalSoluciones    = new DataTable();
                string        vSegmento            = "";
                string        vArea          = "";
                string        vSolucion      = "";
                string        vNuevaArea     = "";
                string        vTotArea       = "";
                string        vTotSolucion   = "";
                string        vAreaID        = "";
                string        vSolucionID    = "";
                string        vNuevaSolucion = "";
                Double        vpTotal        = 0;
                Double        vTotSegmento   = 0;
                DataRow       drEstructura;
                CN_CatCliente cn_catcliente = new CN_CatCliente();
                Clientes      cte           = new Clientes();
                cte.Id_Emp = session.Id_Emp;
                cte.Id_Cd  = session.Id_Cd_Ver;
                cte.Id_Cte = Id_Cte;
                cte.Id_Seg = Id_Seg;

                cn_catcliente.EstructuraSegmento(ref dsEstructuraSegmento, cte, session.Emp_Cnx);
                if (dsEstructuraSegmento != null)
                {
                    if (dsEstructuraSegmento.Tables[0].Rows.Count != 0)
                    {
                        dtTotalAreas      = dsEstructuraSegmento.Tables[1];
                        dtTotalSoluciones = dsEstructuraSegmento.Tables[2];

                        //Leer estructura del Segmento
                        tablahtml.Append("<table cellspacing=\"1\" rules=\"all\" border=\"1\" id=\"DataGrid1\" style=\"background-color:White;width:900px;\">");
                        tablahtml.Append("<tr class=\"tr_tit\">");
                        tablahtml.Append("<th scope=\"col\">UEN</th>");
                        tablahtml.Append("<th scope=\"col\">Segmento</th>");
                        tablahtml.Append("<th scope=\"col\">Área</th>");
                        tablahtml.Append("<th scope=\"col\">Solución</th>");
                        tablahtml.Append("<th scope=\"col\">Aplicación</th>");
                        tablahtml.Append("<th scope=\"col\">Potencial teórico</th>");
                        tablahtml.Append("<th scope=\"col\">Potencial observado</th>");
                        tablahtml.Append("</tr>");

                        vTotArea = dsEstructuraSegmento.Tables[1].Rows.Count.ToString();
                        if (vTotArea == "1")
                        {
                            vArea = dsEstructuraSegmento.Tables[1].Rows[0]["TotalArea"].ToString();
                        }

                        for (int i = 0; i <= dsEstructuraSegmento.Tables[0].Rows.Count - 1; i++)
                        {
                            drEstructura = dsEstructuraSegmento.Tables[0].Rows[i];
                            vSegmento    = dsEstructuraSegmento.Tables[0].Rows.Count.ToString();
                            //        'vArea = dsEstructuraSegmento.Tables(0).Rows(i)("AreaID")
                            if (i == 0)
                            {
                                tablahtml.Append("<tr>");
                                tablahtml.Append("<td rowspan=" + vSegmento + "> " + txtUEN.Text + "</td>");
                                tablahtml.Append("<td rowspan=" + vSegmento + "> " + txtSegmento.Text + "</td>");
                                vAreaID = dsEstructuraSegmento.Tables[1].Rows[i]["AreaID"].ToString();
                                if (vTotArea == "1")
                                {
                                    tablahtml.Append("<td rowspan=" + vArea + "> " + drEstructura["Area"] + "</td>");
                                }
                                else
                                {
                                    for (int j = 0; j <= dtTotalAreas.Rows.Count - 1; j++)
                                    {
                                        if (vAreaID == dtTotalAreas.Rows[j]["AreaID"].ToString())
                                        {
                                            vTotArea = dtTotalAreas.Rows[j]["TotalArea"].ToString();
                                            vArea    = dtTotalAreas.Rows[j]["Area"].ToString();
                                            break;
                                        }
                                    }
                                    tablahtml.Append("<td rowspan=" + vTotArea + "> " + vArea + "</td>");
                                }
                                vSolucionID = dsEstructuraSegmento.Tables[0].Rows[i]["SolucionID"].ToString();
                                for (int j = 0; j <= dtTotalSoluciones.Rows.Count - 1; j++)
                                {
                                    if (vSolucionID == dtTotalSoluciones.Rows[j]["SolucionID"].ToString())
                                    {
                                        vTotSolucion = dtTotalSoluciones.Rows[j]["TotalSolucion"].ToString();
                                        vSolucion    = dtTotalSoluciones.Rows[j]["Solucion"].ToString();
                                        break;
                                    }
                                }
                                tablahtml.Append("<td rowspan=" + vTotSolucion + "> " + vSolucion + "</td>");
                            }
                            else
                            {
                                //''''''''''''''''''''
                                tablahtml.Append("<tr>");
                                //''''''''''''''''''''
                                vNuevaArea = dsEstructuraSegmento.Tables[0].Rows[i]["AreaID"].ToString();
                                if (vNuevaArea != vAreaID)
                                {
                                    vAreaID = vNuevaArea;
                                    for (int j = 0; j <= dtTotalSoluciones.Rows.Count - 1; j++)
                                    {
                                        if (vAreaID == dtTotalAreas.Rows[j]["AreaID"].ToString())
                                        {
                                            vTotArea = dtTotalAreas.Rows[j]["TotalArea"].ToString();
                                            vArea    = dtTotalAreas.Rows[j]["Area"].ToString();
                                            break;
                                        }
                                    }
                                    tablahtml.Append("<td rowspan=" + vTotArea + "> " + vArea + "</td>");
                                }
                                //            '''''''''''''''''''''''''
                                vNuevaSolucion = dsEstructuraSegmento.Tables[0].Rows[i]["SolucionID"].ToString();
                                if (vNuevaSolucion != vSolucionID)
                                {
                                    vSolucionID = vNuevaSolucion;
                                    for (int j = 0; j <= dtTotalSoluciones.Rows.Count - 1; j++)
                                    {
                                        if (vSolucionID == dtTotalSoluciones.Rows[j]["SolucionID"].ToString())
                                        {
                                            vTotSolucion = dtTotalSoluciones.Rows[j]["TotalSolucion"].ToString();
                                            vSolucion    = dtTotalSoluciones.Rows[j]["Solucion"].ToString();
                                            break;
                                        }
                                    }
                                    tablahtml.Append("<td rowspan=" + vTotSolucion + "> " + vSolucion + "</td>");
                                }
                            }
                            vpTotal += Convert.ToDouble(drEstructura["Porcentaje"]) * (Convert.ToDouble(txtValor.Text) * Convert.ToDouble(txtFactor.Text));
                            tablahtml.Append("<td> " + drEstructura["Aplicacion"].ToString() + "</td>");
                            tablahtml.Append("<td> " + (Convert.ToDouble(drEstructura["Porcentaje"]) * Convert.ToDouble(txtValor.Text) * Convert.ToDouble(txtFactor.Text)).ToString("$ #,##0.00") + "</td>");
                            //        ' tablahtml.Append("<td> " + for (matCurrency((drEstructura("Porcentaje") * (Me.txtValor.Text * Me.txtFactor.Text)), TriState.True, TriState.True) + "</td>")
                            //        'Verif (icando si hay valor potencial teorico cambiado
                            double vTeorico = 0;

                            if (dsEstructuraSegmento.Tables[3].Rows.Count != 0)
                            {
                                for (int m = 0; m <= dsEstructuraSegmento.Tables[3].Rows.Count - 1; m++)
                                {
                                    if (drEstructura["AplicacionID"] == dsEstructuraSegmento.Tables[3].Rows[m]["AplicacionID"])
                                    {
                                        vTeorico = Convert.ToDouble(dsEstructuraSegmento.Tables[3].Rows[m]["VPTeorico"]);
                                        break;
                                    }
                                    else
                                    {
                                        vTeorico = 0;
                                    }
                                }
                            }
                            else
                            {
                                vTeorico = 0;
                            }

                            if (vTeorico == 0)
                            {
                                vTeorico = Convert.ToDouble(drEstructura["Porcentaje"]) * Convert.ToDouble(txtValor.Text) * Convert.ToDouble(txtFactor.Text);
                            }

                            vTotSegmento += vTeorico;
                            switch (i)
                            {
                            case 1:
                                tablahtml.Append("<td><input id=\"txtApp1\" runat=\"server\" type=\"text\" size=\"10\" value=" + vTeorico.ToString("$ #,##0.00") + ">" + "</td>");
                                break;

                            case 2:
                                tablahtml.Append("<td><input id=\"txtApp2\" runat=\"server\" type=\"text\" size=\"10\" value=" + vTeorico.ToString("$ #,##0.00") + ">" + "</td>");
                                break;

                            case 3:
                                tablahtml.Append("<td><input id=\"txtApp3\" runat=\"server\" type=\"text\" size=\"10\" value=" + vTeorico.ToString("$ #,##0.00") + ">" + "</td>");
                                break;

                            case 4:
                                tablahtml.Append("<td><input id=\"txtApp4\" runat=\"server\" type=\"text\" size=\"10\" value=" + vTeorico.ToString("$ #,##0.00") + ">" + "</td>");
                                break;

                            default:
                                tablahtml.Append("<td><input id=\"txtApp\" runat=\"server\" type=\"text\" size=\"10\" value=" + vTeorico.ToString("$ #,##0.00") + ">" + "</td>");
                                break;
                            }
                            tablahtml.Append("</tr>");
                        }
                    }
                }
                txtValorPO.Text = vTotSegmento.ToString("$ #,##0.00");
                tablahtml.Append("</table>");
                lblEstruct.Text = tablahtml.ToString();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private void LeerEstructura(int?Id_Cte, int?Id_Seg)
        {
            try
            {
                int           vTotalGralAreas      = 0;
                DataSet       dsEstructuraSegmento = new DataSet();
                CN_CatCliente cn_catcliente        = new CN_CatCliente();
                Clientes      cte = new Clientes();
                cte.Id_Emp = session.Id_Emp;
                cte.Id_Cd  = session.Id_Cd_Ver;
                cte.Id_Cte = Id_Cte;
                cte.Id_Seg = Id_Seg;
                cn_catcliente.EstructuraSegmento(ref dsEstructuraSegmento, cte, session.Emp_Cnx);
                DataTable dtTotalAreas      = new DataTable();
                DataTable dtTotalSoluciones = new DataTable();

                DataGrid1.DataSource = dsEstructuraSegmento.Tables[0];
                DataGrid1.DataBind();
                dg2.DataSource = dsEstructuraSegmento.Tables[0];
                dg2.DataBind();

                dtTotalAreas      = dsEstructuraSegmento.Tables[1];
                dtTotalSoluciones = dsEstructuraSegmento.Tables[2];

                if (DataGrid1.Items.Count == 0)
                {
                    return;
                }


                this.DataGrid1.Items[0].Cells[0].RowSpan = this.DataGrid1.Items.Count;
                this.DataGrid1.Items[0].Cells[1].RowSpan = this.DataGrid1.Items.Count;

                vTotalGralAreas = dtTotalAreas.Rows.Count;

                for (int i = 1; i <= this.DataGrid1.Items.Count - 1; i++)
                {
                    if (i == 1)
                    {
                        this.DataGrid1.Items[0].Cells[5].Text = (Convert.ToDouble(this.DataGrid1.Items[0].Cells[5].Text) * Convert.ToDouble(this.txtValor.Text) * Convert.ToDouble(this.txtFactor.Text)).ToString("$ #,##0.00");
                    }

                    this.DataGrid1.Items[i].Cells[5].Text = (Convert.ToDouble(this.DataGrid1.Items[i].Cells[5].Text) * Convert.ToDouble(this.txtValor.Text) * Convert.ToDouble(this.txtFactor.Text)).ToString("$ #,##0.00");
                    this.DataGrid1.Items[i].Cells.RemoveAt(0);
                    this.DataGrid1.Items[i].Cells.RemoveAt(0);
                }

                int totSoluciones     = 0;
                int vSolucionAnterior = 0;
                int totAnterior       = 0;
                int totAreas          = 0;
                int vAreaAnterior     = 0;
                int totAnteriorA      = 0;

                //AREAS
                for (int i = 0; i <= dtTotalAreas.Rows.Count - 1; i++)
                {
                    if (i == 0)
                    {
                        if (vAreaAnterior != (int)dtTotalAreas.Rows[i]["AreaID"])
                        {
                            vAreaAnterior = (int)dtTotalAreas.Rows[i]["AreaID"];
                            totAreas      = (int)dtTotalAreas.Rows[i]["TotalArea"];
                            this.DataGrid1.Items[totAnteriorA].Cells[2].RowSpan = totAreas;
                            for (int j = totAnteriorA + 1; j <= (totAnteriorA + totAreas) - 1; j++)
                            {
                                this.DataGrid1.Items[j].Cells.RemoveAt(0);
                            }

                            totAnteriorA = totAreas;
                        }
                    }
                    else if (i >= 1)
                    {
                        vAreaAnterior = (int)dtTotalAreas.Rows[i]["AreaID"];
                        totAreas      = (int)dtTotalAreas.Rows[i]["TotalArea"];
                        this.DataGrid1.Items[totAnteriorA].Cells[0].RowSpan = totAreas;
                        for (int j = totAnteriorA + 1; j <= (totAnteriorA + totAreas) - 1; j++)
                        {
                            this.DataGrid1.Items[j].Cells.RemoveAt(0);
                        }

                        totAnteriorA = totAnteriorA + totAreas;
                    }
                }

                //SOLUCIONES
                for (int i = 0; i <= dtTotalSoluciones.Rows.Count - 1; i++)
                {
                    if (i == 0)
                    {
                        if (vSolucionAnterior != (int)dtTotalSoluciones.Rows[i]["SolucionID"])
                        {
                            vSolucionAnterior = (int)dtTotalSoluciones.Rows[i]["SolucionID"];
                            totSoluciones     = (int)dtTotalSoluciones.Rows[i]["TotalSolucion"];
                            this.DataGrid1.Items[totAnterior].Cells[3].RowSpan = totSoluciones;
                            for (int j = totAnterior + 1; j <= (totAnterior + totSoluciones) - 1; j++)
                            {
                                this.DataGrid1.Items[j].Cells.RemoveAt(0);
                            }

                            totAnterior = totSoluciones;
                        }
                    }
                    else if (i >= 1)
                    {
                        vSolucionAnterior = (int)dtTotalSoluciones.Rows[i]["SolucionID"];
                        totSoluciones     = (int)dtTotalSoluciones.Rows[i]["TotalSolucion"];
                        this.DataGrid1.Items[totAnterior].Cells[1].RowSpan = totSoluciones;
                        for (int j = totAnterior + 1; j <= (totAnterior + totSoluciones) - 1; j++)
                        {
                            this.DataGrid1.Items[j].Cells.RemoveAt(0);
                        }

                        totAnterior = totAnterior + totSoluciones;
                    }
                }

                //APLICACIONES
                for (int i = 0; i <= dtTotalSoluciones.Rows.Count - 1; i++)
                {
                    int vAplicacionID = 0;
                    //    'Escribiendo el VPTeorico de la Aplicacion
                    if (dsEstructuraSegmento.Tables[3].Rows.Count != 0)
                    {
                        // 'Escribiendo valores potenciales observados
                        for (int p = 0; p <= this.dg2.Items.Count - 1; p++)
                        {
                            RadNumericTextBox txt = new RadNumericTextBox();
                            txt = (RadNumericTextBox)this.DataGrid1.Items[p].FindControl("txt");
                            try
                            {
                                txt.Value = (Convert.ToDouble(this.dg2.Items[p].Cells[5].Text) * Convert.ToDouble(this.txtValor.Text) * Convert.ToDouble(this.txtFactor.Text));
                            }
                            catch (Exception ex)
                            {
                                throw ex;
                            }
                        }
                        //'''''''''''''''''''''''''''''''''''''''''''
                        for (int k = 0; k <= this.dg2.Items.Count - 1; k++)
                        {
                            vAplicacionID = (int)this.dg2.DataKeys[k];
                            for (int j = 0; j <= dsEstructuraSegmento.Tables[3].Rows.Count - 1; j++)
                            {
                                RadNumericTextBox txt = new RadNumericTextBox();
                                txt = (RadNumericTextBox)this.DataGrid1.Items[k].FindControl("txt");
                                if ((int)dsEstructuraSegmento.Tables[3].Rows[j]["AplicacionID"] == vAplicacionID)
                                {
                                    txt.Value = Convert.ToDouble(dsEstructuraSegmento.Tables[3].Rows[j]["VPTeorico"]);
                                }
                            }
                        }
                    }
                    else
                    {
                        for (int j = 0; j <= this.dg2.Items.Count - 1; j++)
                        {
                            RadNumericTextBox txt = new RadNumericTextBox();
                            txt       = (RadNumericTextBox)this.DataGrid1.Items[j].FindControl("txt");
                            txt.Value = (Convert.ToDouble(this.dg2.Items[j].Cells[5].Text) * Convert.ToDouble(this.txtValor.Text) * Convert.ToDouble(this.txtFactor.Text));
                        }
                    }
                }
                CopiaValoresPotenciales();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }