-->

CRUD dan Report Viewer Pada Visual Studio

Apa itu CRUD ??
CRUD merupakan button button yang biasa digunakan dalam program visual studi, dengan C yang berarti Create, R yang berarti Read, U yang berarti Update dan D yang berarti Delete. Sedangakn Report Viewer merupakan salah satu item yang terdapat pada toolbox.


Pertama-tama kita harus membuat 3 form, dengan form 1 mdi parent, form 2 design program yang diinginkan, dan form 3 report viewer.

1. Form 1

Keterangan Interface
No.
Nama Komponen
Variabel Komponen
1
Menu Strip
programToolStripMenuItem
2
Menu Strip
printToolStripMenuItem
3
Menu Strip
exitToolStripMenuItem


2. Form 2

Keterangan Interface
No.
Nama Komponen
Variabel Komponen
1
Label
Lbljual
2
Label
Lblket
3
Label
Lblnomor
4
Label
Lblnama
5
Label
Lblmerk
6
Label
Lblharga
7
Button
Btncreate
8
Button
btnupdate
9
Text Box
Txtnomor
10
Text Box
Txtnama
11
Label
Lblcari
12
Text Box
Txtcari
13
Combo Box
cbmerk
14
Text Box
txtharga
15
Button
btnrefresh
16
Button
btndelete
17
DataGridView
dgv

3. Form 3

Keterangan Interface
No.
Nama Komponen
Variabel Komponen
1
Reportviewer
Reportviewer1
Coding Program
Class Form 1
Nama Komponen
Event
Source Code
programToolStripMenuItem
Click
private void programToolStripMenuItem_Click(object sender, EventArgs e)
        {
           
            Form2 sari = new Form2();
            sari.Refresh(); //memanggil method refresh
            sari.MdiParent = this; //mdi parent
            sari.Show(); //menampilkan
        }
exitToolStripMenuItem
Click
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Anda yakin ingin keluar ?", "Awas!", MessageBoxButtons.OKCancel); if (dr == DialogResult.OK) //peringatan sebelum keluar program
            {
                this.Close(); //menutup program
            }

        }
printToolStripMenuItem
click
private void printToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form3 sari = new Form3(); //memanggil form3
            sari.Refresh() ; //memanggil method refresh
            sari.MdiParent = this; //mdi parent
            sari.Show(); //menampilkan sari

        }
Class Form 2
Nama Komponen
Event
Source Code
Form2
Load
private void Form2_Load(object sender, EventArgs e)
        {
            gridview_databind(); //method gridview
            reset(); //method reset
        }
btncreate
Click
private void btncreate_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection conn = kon.GetConn(); //sql konek

                conn.Open(); //membuka sql
                string query = string.Format("insert into tabel_jual values('{0}','{1}','{2}')",txtnama.Text, cbmerk.SelectedItem.ToString(), txtharga.Text); //jumlah tabel
                SqlCommand command = new SqlCommand(query, conn);
                command.ExecuteNonQuery();
                conn.Close(); //menutup sql

                MessageBox.Show("Data berhasil disimpan"); //peringatan data berhasil disimpan
                gridview_databind(); //method gridview
                reset(); //method refresh
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

txtsearch
txtchanged
private void txtsearch_TextChanged(object sender, EventArgs e)
        {
            try
            {
                SqlConnection conn = kon.GetConn();

                conn.Open();
                string query = "select nomor as NOMOR, nama_pembeli as NAMA_PEMBELI, merk_hp as MERK_HP, harga as HARGA from tabel_jual WHERE nomor LIKE '%" + txtsearch.Text + "%' OR nama_pembeli LIKE '%" + txtsearch.Text + "%' OR merk_hp LIKE '%" + txtsearch.Text + "%' OR harga LIKE '%" + txtsearch.Text + "%'"; //pencarian
                SqlCommand command = new SqlCommand(query, conn);
                SqlDataAdapter sda = new SqlDataAdapter(command);
                DataTable dt = new DataTable();

                sda.Fill(dt);
                dgv.DataSource = dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
btnupdate
Click
private void btnupate_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection conn = kon.GetConn();
                conn.Open();
                var query = "update tabel_jual set nama_pembeli='" + txtnama.Text + "',merk_hp='" + cbmerk.Text + "',harga='" + txtharga.Text + "' where nomor='" + txtnomor.Text + "'";
                var command = new SqlCommand(query, conn);
                command.ExecuteNonQuery();
                MessageBox.Show("Data Berhasil Diedit", "Pemberitahuan", MessageBoxButtons.OK, MessageBoxIcon.Information); //peringatan data berhasil diedit
                command.Dispose();
                conn.Close(); //menutup program
                gridview_databind(); //method
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
btndelete
Click
private void btndelete_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection conn = kon.GetConn();

                conn.Open();

                var nomor = dgv.CurrentRow.Cells[0].Value;
                var query = "delete from tabel_jual where nomor='" + nomor + "'";
                SqlCommand command = new SqlCommand(query, conn);
        command.ExecuteNonQuery();
                conn.Close();
              MessageBox.Show("Data berhasil dihapus"); //data berhasil disimpan
               gridview_databind(); //method
            }
            catch (Exception ex)
            {    
        MessageBox.Show(ex.ToString());
            }
        }
dgv
cellclick
private void dgv_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewRow row = dgv.Rows[e.RowIndex];
            txtnomor.Text = row.Cells[0].Value.ToString();
            txtnama.Text = row.Cells[1].Value.ToString();
            cbmerk.Text = row.Cells[2].Value.ToString();
            txtharga.Text = row.Cells[3].Value.ToString();

        } //menampilkan data pada datagirdview
btnrefresh
click
private void btnrefresh_Click(object sender, EventArgs e)
        {
            reset(); //method
        }
Class Form 3
Nama Komponen
Event
Source Code
Form3
Load
private void Form3_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'Database1DataSet.tabel_jual' table. You can move, or remove it, as needed.           this.tabel_jualTableAdapter.Fill(this.Database1DataSet.tabel_jual);
            this.reportViewer1.RefreshReport();

        } //report viewer


Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel