コード例 #1
0
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            //MessageBox.Show(dataGridView1.Columns[e.ColumnIndex].Name);
            if (e.ColumnIndex > -1)
            {
                int examen = Convert.ToInt32(dataGridView1.Columns[e.ColumnIndex].Name);
                //SP_ExamenPracticado_ImagenTableAdapter.Connection
                this.SP_ExamenPracticado_ImagenTableAdapter.Fill(this.HistoriaClinica_New.SP_ExamenPracticado_Imagen, NumeroAtencion, examen);
                //Creamos los parametros para la atenciòn
                ReportParameter Titulo = new ReportParameter();
                ReportParameter nombre = new ReportParameter();

                //Establecemos el valor de los parámetros de la informacion personal
                Titulo = new ReportParameter("Titulo", TxtTitulo.Text);
                nombre = new ReportParameter("Nombre", NombreCompleto);

                //--(DATOS ANTECEDENTES GINECOLOGICOS)
                reportViewer1.LocalReport.EnableExternalImages = true;
                this.reportViewer1.ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Local;

                this.reportViewer1.LocalReport.SetParameters(Titulo);
                this.reportViewer1.LocalReport.SetParameters(nombre);

                ClsGuardarImagen ObjImagen = new ClsGuardarImagen();
                string           ruta      = ObjImagen.RutaImagen(LblDocumento.Text, NumeroAtencion.ToString(), examen.ToString());
                //MessageBox.Show(ruta);
                if (ruta == "")
                {
                    ReportParameter paramImagen = new ReportParameter("rutaImagen", "", true);
                    reportViewer1.LocalReport.SetParameters(paramImagen);
                }
                else
                {
                    ReportParameter paramImagen = new ReportParameter("rutaImagen", "file:" + ruta, true);
                    reportViewer1.LocalReport.SetParameters(paramImagen);
                }

                this.reportViewer1.RefreshReport();
                //reportViewer1.DataBindings=Repor
                reportViewer1.SetDisplayMode(DisplayMode.PrintLayout);
                reportViewer1.ZoomMode = ZoomMode.Percent;
                //Seleccionamos el zoom que deseamos utilizar. En este caso un 100%
                reportViewer1.ZoomPercent = 100;
            }
        }
        private void BtnGuardarHistoria_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("¿Esta seguro de guardar la Informacón? ", "Confirmar", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            {
                ObjServer.CadenaCnn = Conexion.CadenaConexion.cadena();
                ObjServer.Conectar();

                //:::::::::Comienzo de la transacción:::::::::::::
                //Establecemos el Objeto que nos va a permitir conectarnos a la base de Datos()
                SqlConnection cnn = new SqlConnection(Conexion.CadenaConexion.cadena());
                //Abrimos la conexión()
                cnn.Open();
                //Comenzamos la transacción ()
                SqlTransaction SQLtrans = cnn.BeginTransaction();
                try
                {
                    SqlCommand comman = cnn.CreateCommand();
                    comman.Transaction = SQLtrans;

                    //ELIMINAR LOS EXAMENES PRACTICADOS
                    string QueryI = "DELETE FROM [dbo].[ExamenPracticado]  WHERE ExaPrac_Entrada_Numero=" + Convert.ToInt32(LblNumeroAtencion.Text);
                    comman.CommandText = QueryI;
                    comman.ExecuteNonQuery();

                    string[] fecha = DateTime.Now.ToString().Split(' ');
                    //PRIME SE INSERTA LA ENTRADA - INSERTAR EN ENTRADA - HISTORIA
                    string Query;

                    //Insertar EXAMEN PRACTICADOS
                    System.IO.MemoryStream ms = new System.IO.MemoryStream();
                    for (int i = 0; i < DgvExamenPacticado.Rows.Count - 1; i++)
                    {
                        string Imagen = "";
                        Imagen = DgvExamenPacticado.Rows[i].Cells[3].Value.ToString();
                        //MessageBox.Show(Imagen);
                        if (Imagen != "System.Byte[]")
                        {
                            if (DgvExamenPacticado.Rows[i].Cells["DgvExamenPacticadoColAjuntar"].Value != null)
                            {
                                //if (DgvExamenPacticado.Rows[i].Cells["DgvExamenPacticadoColAjuntar"].Value.ToString().Length > 7 && DgvExamenPacticado.Rows[i].Cells["DgvExamenPacticadoColExamen"].Value != null)
                                //{
                                Boolean confirmar = false;         //Para saber si hay datos para agregar
                                int     CodExamen = 0;
                                string  FechaExa  = "";
                                int     resultado = 0;

                                if (DgvExamenPacticado.Rows[i].Cells["DgvExamenPacticadoColResultado"].Value != null)
                                {
                                    resultado = Convert.ToInt32(DgvExamenPacticado.Rows[i].Cells["DgvExamenPacticadoColResultado"].Value);
                                }
                                else
                                {
                                    resultado = 1;
                                }

                                FechaExa  = DgvExamenPacticado.Rows[i].Cells["DgvExamenPacticadoColFecha"].Value.ToString();
                                CodExamen = Convert.ToInt32(DgvExamenPacticado.Rows[i].Cells["DgvExamenPacticadoColExamen"].Value);
                                confirmar = true;
                                Query     = "INSERT INTO [dbo].[ExamenPracticado] VALUES (@examen" + i + ",@NumAtencion" + i + ",@resultado" + i + ",@foto" + i + ",@fecha" + i + ")";

                                comman.CommandText = Query;
                                comman.Parameters.Add("@NumAtencion" + i, SqlDbType.Int);
                                comman.Parameters.Add("@examen" + i, SqlDbType.Int);
                                comman.Parameters.Add("@resultado" + i, SqlDbType.Int);
                                comman.Parameters.Add("@fecha" + i, SqlDbType.Date);
                                comman.Parameters["@NumAtencion" + i].Value = Convert.ToInt32(LblNumeroAtencion.Text);
                                comman.Parameters["@examen" + i].Value      = CodExamen;
                                comman.Parameters["@resultado" + i].Value   = resultado;
                                comman.Parameters["@fecha" + i].Value       = FechaExa;

                                if (DgvExamenPacticado.Rows[i].Cells["DgvExamenPacticadoColDireccion"].Value != null)
                                {
                                    ClsGuardarImagen ObjGuardarImagen = new ClsGuardarImagen();
                                    string           direcion         = DgvExamenPacticado.Rows[i].Cells["DgvExamenPacticadoColDireccion"].Value.ToString();
                                    string[]         extencion        = DgvExamenPacticado.Rows[i].Cells[3].Value.ToString().Split('.');
                                    string           nombreexamen     = DgvExamenPacticado.Rows[i].Cells["DgvExamenPacticadoColExamen"].Value.ToString();
                                    //MessageBox.Show(txtidentificacion.Text + " - " + LblNumeroAtencion.Text + " - " + nombreexamen + "." + extencion[1] + " - " + direcion);
                                    ObjGuardarImagen.CrearCarpeta(txtidentificacion.Text, LblNumeroAtencion.Text, nombreexamen + "." + extencion[1], direcion);
                                }

                                SqlParameter imageParameter = new SqlParameter("@foto" + i, SqlDbType.Image);
                                imageParameter.Value = DBNull.Value;
                                comman.Parameters.Add(imageParameter);
                                comman.ExecuteNonQuery();
                            }
                        }
                        else
                        {
                            int    CodExamen = 0;
                            string FechaExa  = "";
                            int    resultado = 0;

                            if (DgvExamenPacticado.Rows[i].Cells["DgvExamenPacticadoColResultado"].Value != null)
                            {
                                resultado = Convert.ToInt32(DgvExamenPacticado.Rows[i].Cells["DgvExamenPacticadoColResultado"].Value);
                            }
                            else
                            {
                                resultado = 1;
                            }

                            FechaExa = DgvExamenPacticado.Rows[i].Cells["DgvExamenPacticadoColFecha"].Value.ToString();

                            CodExamen = Convert.ToInt32(DgvExamenPacticado.Rows[i].Cells["DgvExamenPacticadoColExamen"].Value);
                            Query     = "INSERT INTO [dbo].[ExamenPracticado] VALUES (@examen" + i + ",@NumAtencion" + i + ",@resultado" + i + ",@foto" + i + ",@fecha" + i + ")";

                            comman.CommandText = Query;
                            comman.Parameters.Add("@NumAtencion" + i, SqlDbType.Int);
                            comman.Parameters.Add("@examen" + i, SqlDbType.Int);
                            comman.Parameters.Add("@resultado" + i, SqlDbType.Int);
                            comman.Parameters.Add("@fecha" + i, SqlDbType.Date);

                            comman.Parameters["@NumAtencion" + i].Value = Convert.ToInt32(LblNumeroAtencion.Text);
                            comman.Parameters["@examen" + i].Value      = CodExamen;
                            comman.Parameters["@resultado" + i].Value   = resultado;
                            comman.Parameters["@fecha" + i].Value       = FechaExa;

                            if (DgvExamenPacticado.Rows[i].Cells["DgvExamenPacticadoColAjuntar"].Value != null)
                            {
                                comman.Parameters.Add("@foto" + i, SqlDbType.Image);
                                string Imagen2 = DgvExamenPacticado.Rows[i].Cells["DgvExamenPacticadoColAjuntar"].Value.ToString();
                                PctFoto.Image = null;
                                ms            = new MemoryStream();
                                if (Imagen2 == "System.Byte[]")
                                {
                                    byte[] imageBuffer = (byte[])DgvExamenPacticado.Rows[i].Cells["DgvExamenPacticadoColAjuntar"].Value;
                                    // Se crea un MemoryStream a partir de ese buffer
                                    System.IO.MemoryStream ms1 = new System.IO.MemoryStream(imageBuffer);
                                    // Se utiliza el MemoryStream para extraer la imagen
                                    this.PctFoto.Image = Image.FromStream(ms1);
                                    PctFoto.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                                }
                                else
                                {
                                    this.PctFoto.Image = Image.FromFile(DgvExamenPacticado.Rows[i].Cells["DgvExamenPacticadoColAjuntar"].Value.ToString());
                                    PctFoto.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                                }
                                comman.Parameters["@foto" + i].Value = ms.GetBuffer();
                            }
                            else
                            {
                                SqlParameter imageParameter = new SqlParameter("@foto", SqlDbType.Image);
                                imageParameter.Value = DBNull.Value;
                                comman.Parameters.Add(imageParameter);
                            }
                            comman.ExecuteNonQuery();
                        }
                    }
                    //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

                    SQLtrans.Commit();
                    //DgvExamenPacticado.Rows.Clear();
                    CargarIconosDGV();
                    MessageBox.Show("Datos guardados correctamente", "finalizar", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("La operación no puedo completarse debido a: \n 1 - No dispone de una conexión  \n 2 - El registro esta duplicado  \n 3 - La información ingrasado no corresponde a la requerida  \n 4 - No se ha cargado imagen  \n Vuelva a intentarlo!!! " + ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    //MessageBox.Show(ex.ToString());
                    SQLtrans.Rollback();
                }
            }
        }
        private void DgvExamenPacticado_CellClick_1(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                if (e.ColumnIndex == 4)
                {
                    // Se crea el OpenFileDialog
                    OpenFileDialog dialog = new OpenFileDialog();
                    // Se muestra al usuario esperando una acción
                    DialogResult result = dialog.ShowDialog();

                    // Si seleccionó un archivo (asumiendo que es una imagen lo que seleccionó)
                    // la mostramos en el PictureBox de la inferfaz
                    if (result == DialogResult.OK)
                    {
                        DgvExamenPacticado.CurrentRow.Cells[3].Value = (dialog.SafeFileName);
                        DgvExamenPacticado.CurrentRow.Cells["DgvExamenPacticadoColDireccion"].Value = dialog.FileName;
                        DgvExamenPacticado.CurrentCell.Value = "Imagen #" + (e.RowIndex + 1).ToString();
                        CargarFecha();
                    }
                }
                if (e.ColumnIndex == 6)
                {
                    try
                    {
                        string Imagen = "";
                        Imagen = DgvExamenPacticado.Rows[DgvExamenPacticado.CurrentCell.RowIndex].Cells[3].Value.ToString();
                        //MessageBox.Show(Imagen);
                        if (Imagen == "System.Byte[]")
                        {
                            byte[] imageBuffer = (byte[])DgvExamenPacticado.Rows[DgvExamenPacticado.CurrentCell.RowIndex].Cells[3].Value;
                            // Se crea un MemoryStream a partir de ese buffer
                            System.IO.MemoryStream ms1 = new System.IO.MemoryStream(imageBuffer);
                            // Se utiliza el MemoryStream para extraer la imagen
                            this.PctFoto.Image = Image.FromStream(ms1);
                        }
                        else
                        {
                            this.PctFoto.Image = Image.FromFile(Imagen);
                            //PctFoto.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                        }
                        string RutaArchivoDestino = Application.StartupPath + @"\imagenes\NombreArch.jpg";
                        File.Delete(RutaArchivoDestino);
                        //string Imagen = Application.StartupPath + @"\imagenes\doctor.png";
                        string ruta = Path.Combine(Application.StartupPath + @"\imagenes", "NombreArch.jpg");
                        PctFoto.Image.Save(ruta, ImageFormat.Jpeg);

                        //string ruta_imagen = @"C:\User\foto.jpg";
                        System.Diagnostics.Process          process   = new System.Diagnostics.Process();
                        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
                        startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                        startInfo.FileName    = "cmd.exe";

                        startInfo.Arguments = @"/c rundll32 ""C:\Program Files\Windows Photo Viewer\PhotoViewer.dll"", ImageView_Fullscreen " + RutaArchivoDestino;
                        process.StartInfo   = startInfo;
                        process.Start();
                    }
                    catch (Exception)
                    {
                        try
                        {
                            ClsGuardarImagen ObjGuardarImagen = new ClsGuardarImagen();

                            if (DgvExamenPacticado.Rows[e.RowIndex].Cells["DgvExamenPacticadoColDireccion"].Value == null)
                            {
                                string nombreexamen = DgvExamenPacticado.Rows[e.RowIndex].Cells["DgvExamenPacticadoColExamen"].Value.ToString();
                                ObjGuardarImagen.AbrirIMG(txtidentificacion.Text, LblNumeroAtencion.Text, nombreexamen);
                            }
                            else
                            {
                                MessageBox.Show("Inicio");
                                ObjGuardarImagen.AbrirIMG(DgvExamenPacticado.Rows[e.RowIndex].Cells["DgvExamenPacticadoColDireccion"].Value.ToString());
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("No hay imagen " + ex.ToString(), "", MessageBoxButtons.OK);
                        }
                    }
                }
            }

            if (e.ColumnIndex == DgvExamenPacticadoColEliminar.Index)
            {
                if (e.RowIndex > -1)
                {
                    try
                    {
                        if (DgvExamenPacticado.Rows.Count > 1)
                        {
                            DgvExamenPacticado.Rows.RemoveAt(DgvExamenPacticado.CurrentRow.Index);
                        }
                        else
                        {
                            DgvExamenPacticado.Rows[DgvExamenPacticado.CurrentRow.Index].Cells["DgvExamenPacticadoColAjuntar2"].Value  = ".....";
                            DgvExamenPacticado.Rows[DgvExamenPacticado.CurrentRow.Index].Cells["DgvExamenPacticadoColResultado"].Value = 1;
                            CargarFecha();
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
            }
        }