private void examesPaciente() { try { conn.Open(); com.Connection = conn; SqlCommand cmd = new SqlCommand("select tipo.nome, exame.data, exame.designacao, exame.observacoes from tipoExame tipo JOIN Exame exame ON tipo.IdTipoExame = exame.idTipoExame WHERE idPaciente = @IdPaciente", conn); cmd.Parameters.AddWithValue("@IdPaciente", paciente.IdPaciente); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { string data = DateTime.ParseExact(reader["data"].ToString(), "dd/MM/yyyy HH:mm:ss", null).ToString("dd/MM/yyyy"); ExamePaciente examePaciente = new ExamePaciente { nome = (string)reader["Nome"], data = data, designacao = (string)reader["designacao"], observacoes = (string)reader["observacoes"], }; listaExamePacientes.Add(examePaciente); } conn.Close(); UpdateDataGridViewExames(); } catch (Exception) { if (conn.State == ConnectionState.Open) { conn.Close(); } MessageBox.Show("Por erro interno é impossível visualizar os dados dos exames!", "Atenção!", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void UpdateDataGridView() { try { examePacientes.Clear(); conn.Open(); com.Connection = conn; SqlCommand cmd = new SqlCommand("select tipo.nome, exame.data, exame.designacao, exame.observacoes from tipoExame tipo JOIN Exame exame ON tipo.IdTipoExame = exame.idTipoExame WHERE idPaciente = @IdPaciente ORDER BY exame.data, tipo.nome", conn); cmd.Parameters.AddWithValue("@IdPaciente", paciente.IdPaciente); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { string data = DateTime.ParseExact(reader["data"].ToString(), "dd/MM/yyyy HH:mm:ss", null).ToString("dd/MM/yyyy"); ExamePaciente examePaciente = new ExamePaciente { nome = (string)reader["Nome"], data = data, designacao = (string)reader["designacao"], observacoes = (string)reader["observacoes"], }; examePacientes.Add(examePaciente); } var bindingSource1 = new System.Windows.Forms.BindingSource { DataSource = examePacientes }; dataGridViewExames.DataSource = bindingSource1; dataGridViewExames.Columns[0].HeaderText = "Exame"; dataGridViewExames.Columns[1].HeaderText = "Data do Exame"; dataGridViewExames.Columns[2].HeaderText = "Designação"; dataGridViewExames.Columns[3].HeaderText = "Observações"; conn.Close(); dataGridViewExames.Update(); dataGridViewExames.Refresh(); } catch (Exception) { if (conn.State == ConnectionState.Open) { conn.Close(); } MessageBox.Show("Por erro interno é impossível selecionar a os exames do paciente!", "Atenção!", MessageBoxButtons.OK, MessageBoxIcon.Error); } }