3
Reply

[Help] Displaying Data in Gridview based on ComboBox selection in C #

Jun Harefa

Jun Harefa

Aug 15 2011 6:08 AM
2.4k
Please help me. I'm making a program for displaying data to a Gridview based on ComboBox selection. Well, the data will be displayed based on three options: Year (cbTahun), Moon (cbBulan), Raw Materials (cbBahan_Baku).
I've managed to display the data if it is selected based on the "Year" or "Month". But I'm still confused if the data is displayed based on the "Raw Materials". I had designed the code but still error.

My Question is the correct code should be like what?, So If I choose based on "Raw Materials" then the data show only the selected raw materials.


Here My Code:

private void button2_Click(object sender, EventArgs e)

        {

            try

            {

                String query = "SELECT kode_order,tanggal_order,lead_time,jumlah,a.kode_barang,c.harga as harga_barang,import_administrasi,import_ekspedisi,import_ijin_import,lokal_administrasi,lokal_transportasi,(import_administrasi+import_ekspedisi+import_ijin_import+lokal_administrasi+lokal_transportasi) as total_ongkos_kirim FROM tb_pemesanan a join tb_ongkos b join tb_bahan_baku c WHERE a.kode_ongkos = b.kode_ongkos AND a.kode_barang=c.kode_barang ";

 

                /*SHOW DATA BASED ON "MONTH"*/

                if (cbBulan.Text != "[semua]")

                {

                    query += " AND MONTH(tanggal_order) = " + cbBulan.Text;

                }

 

                /*SHOW DATA BASED ON "YEAR"*/

                if (cbTahun.Text != "[semua]")

                {

                    query += " AND YEAR(tanggal_order) = " + cbTahun.Text;

                }

 

 

                /*SHOW DATA BASED ON "Raw Material"  (Still Error)*/

                if (cbBahan_Baku.Text != "[semua]")

                {

                    query += " kode_barang = " + cbBahan_Baku.Text;

                }

 

                query += " ORDER BY kode_order asc, tanggal_order ASC ";

 

                DataTable dt = new DataTable();

                MySqlDataAdapter dt_adapter = new MySqlDataAdapter(query, conn);

                MySqlCommandBuilder cb = new MySqlCommandBuilder(dt_adapter);

                dt_adapter.Fill(dt);

                dataGridView1.DataSource = dt;

               

                dataGridView1.Refresh();

            }

            catch (Exception ex)

            {

                MessageBox.Show("Can't load database\n" + ex.ToString());

            }

        }

Answers (3)