using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Prasetiyo
{
public partial class FrmPasien : Form
{
public FrmPasien()
{
InitializeComponent();
}
void ambildata()
{
tyo.tblpasien.Clear();
tyo.dapasien.Fill(tyo.tblpasien);
}
void bersih()
{
tbkodepasien.Clear();
tbnamapasien.Clear();
cbjeniskelamin.SelectedIndex = -1;
cbstatus.SelectedIndex = -1;
cbpilihan.SelectedIndex = -1;
tbcari.Clear();
tbalamat.Clear();
tbkodepasien.Focus();
}
private void FrmPasien_Load(object sender, EventArgs e)
{
this.KeyPreview = true;
tyo.con = tyo.konek_db();
tyo.dapasien = new OleDbDataAdapter
(" select*from pasien", tyo.con);
ambildata();
dataGridView1.DataSource = tyo.tblpasien;
dataGridView1.ReadOnly = true;
dataGridView1.AllowUserToAddRows = false;
dataGridView1.Columns[0].HeaderText = "Kode Pasien";
dataGridView1.Columns[0].Width = 50;
dataGridView1.Columns[1].HeaderText = "Nama Pasien";
dataGridView1.Columns[1].Width = 100;
dataGridView1.Columns[2].HeaderText = "Jenis Kelamin";
dataGridView1.Columns[2].Width = 100;
dataGridView1.Columns[3].HeaderText = "Status";
dataGridView1.Columns[3].Width = 100;
dataGridView1.Columns[4].HeaderText = "Alamat";
dataGridView1.Columns[4].Width = 200;
}
private void bttutup_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Apakah Anda yakin akan menutup menu Pasien?", "Pesan",
MessageBoxButtons.OKCancel,
MessageBoxIcon.Question) == DialogResult.OK)
Close();
}
private void btsimpan_Click(object sender, EventArgs e)
{
tyo.cmd = new OleDbCommand
("select * from pasien where kd_pasien =\'" + tbkodepasien.Text + "\'", tyo.con);
tyo.dtr = tyo.cmd.ExecuteReader();
if (tyo.dtr.HasRows)
{
MessageBox.Show("kode tersebut sudah ada", "pesan", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
tbkodepasien.Clear();
tbkodepasien.Focus();
}
else if (tbkodepasien.Text.Trim() == "")
{
MessageBox.Show("kd_pasien masih kosong", "pesan", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
tbkodepasien.Clear();
tbkodepasien.Focus();
}
else
{
tyo.cmd = new OleDbCommand
("insert into pasien (kd_pasien,nm_pasien,jns_kel,status,alamat)" +
"values(\'" + tbkodepasien.Text + "\'," +
"\'" + tbnamapasien.Text + "\'," +
"\'" + cbjeniskelamin.Text + "\'," +
"\'" + cbstatus.Text + "\'," +
"\'" + tbalamat.Text + "\')", tyo.con);
tyo.cmd.ExecuteNonQuery();
ambildata();
MessageBox.Show("Data udah disimpan", "pesan",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
bersih();
}
}
private void bthapus_Click(object sender, EventArgs e)
{
int brs;
string kode;
brs = dataGridView1.CurrentRow.Index;
kode = dataGridView1[0, brs].Value.ToString();
if (MessageBox.Show("Apakah kd_pasien =" + kode + "\\n" +
"ingin dihapus?", "Pesan",
MessageBoxButtons.OKCancel,
MessageBoxIcon.Question) == DialogResult.OK)
{
tyo.cmd = new OleDbCommand
("delete from PASIEN where kd_pasien=\'" + kode + "\'", tyo.con);
tyo.cmd.ExecuteNonQuery();
ambildata();
MessageBox.Show("Data sudah terhapus", "Pesan",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
}
private void btkoreksi_Click(object sender, EventArgs e)
{
if (btkoreksi.Text == "Koreksi")
{
btkoreksi.Text = "Simpan Perubahan";
btsimpan.Enabled = false;
bthapus.Enabled = false;
tbkodepasien.Enabled = false;
int brsh;
brsh = dataGridView1.CurrentRow.Index;
tbkodepasien.Text = dataGridView1[0, brsh].Value.ToString();
tbnamapasien.Text = dataGridView1[1, brsh].Value.ToString();
cbjeniskelamin.Text = dataGridView1[2, brsh].Value.ToString();
cbstatus.Text = dataGridView1[3, brsh].Value.ToString();
tbalamat.Text = dataGridView1[4, brsh].Value.ToString();
}
else if (btkoreksi.Text == "Simpan Perubahan")
{
btkoreksi.Text = "Koreksi";
btsimpan.Enabled = true;
bthapus.Enabled = true;
tbkodepasien.Enabled = true;
tyo.cmd = new OleDbCommand
("update pasien set " +
"nm_pasien =\'" + tbnamapasien.Text + "\'," +
"jns_kel =\'" + cbjeniskelamin.Text + "\'," +
"status =\'" + cbstatus.Text + "\'," +
"alamat=\'" + tbalamat.Text + "\' where " +
"kd_pasien =\'" + tbkodepasien.Text + "\'", tyo.con);
tyo.cmd.ExecuteNonQuery();
ambildata();
MessageBox.Show("Data sudah di perbaharui", "Pesan",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
bersih();
}
}
private void tbcari_TextChanged(object sender, EventArgs e)
{
if (cbpilihan.SelectedIndex == 0)
{
tyo.dapasien = new OleDbDataAdapter
("select * from pasien where kd_pasien like \'%" +
tbcari.Text + "%\'", tyo.con);
}
else if (cbpilihan.SelectedIndex == 1)
{
tyo.dapasien = new OleDbDataAdapter
("select * from pasien where nm_pasien like \'%" +
tbcari.Text + "%\'", tyo.con);
}
else if (cbpilihan.SelectedIndex == 2)
{
tyo.dapasien = new OleDbDataAdapter
("select * from pasien where jns_kel like \'%" +
tbcari.Text + "%\'", tyo.con);
}
else if (cbpilihan.SelectedIndex == 3)
{
tyo.dapasien = new OleDbDataAdapter
("select * from pasien where status like \'%" +
tbcari.Text + "%\'", tyo.con);
}
else if (cbpilihan.SelectedIndex == 4)
{
tyo.dapasien = new OleDbDataAdapter
("select * from pasien where alamat like \'%" +
tbcari.Text + "%\'", tyo.con);
}
ambildata();
}
private void cbpilihan_SelectedIndexChanged(object sender, EventArgs e)
{
tbcari.Focus();
}
private void btbatal_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Apakah Anda yakin akan membatalkan input data?", "Pesan",
MessageBoxButtons.OKCancel,
MessageBoxIcon.Question) == DialogResult.OK)
bersih();
tyo.dapasien = new OleDbDataAdapter
("select * from pasien", tyo.con);
ambildata();
}
}
}