NYP-2 2016 11. Hafta Uygulamaları

NYP-2 2016 Hafta-11

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 MySql.Data.MySqlClient;

namespace recel
{
    public partial class Form1 : Form
    {
        MySqlDataAdapter da_kalite, da_urun;
        DataTable dt_kalite;

        public Form1()
        {
            InitializeComponent();

            MySqlConnectionStringBuilder bag = new MySqlConnectionStringBuilder();
            bag.Server = "localhost";
            bag.UserID = "root";
            bag.Password = "123456";
            bag.Database = "hafta11_16_2";

            MySqlConnection conn = new MySqlConnection(bag.ToString());

            MySqlCommand cmd_urun_sel = 
                new MySqlCommand("select id,kaliteid,miktar from urun;", conn);
            MySqlCommand cmd_urun_ins = 
                new MySqlCommand("insert into urun(kaliteid,miktar) values(@kaliteid,@miktar);", conn);
            cmd_urun_ins.Parameters.Add("@kaliteid", MySqlDbType.Int32, 11, "kaliteid");
            cmd_urun_ins.Parameters.Add("@miktar", MySqlDbType.Int32, 11, "miktar");

            da_urun = new MySqlDataAdapter();
            da_urun.SelectCommand = cmd_urun_sel;
            da_urun.InsertCommand = cmd_urun_ins;

            MySqlCommand cmd_kalite_sel = 
                new MySqlCommand("select id,ad,oran1,oran2,oran3 from kalite;", conn);
            MySqlCommand cmd_kalite_ins =
                new MySqlCommand("insert into kalite(ad,oran1,oran2,oran3) values(@ad,@oran1,@oran2,@oran3);", conn);
            cmd_kalite_ins.Parameters.Add("@ad",MySqlDbType.VarChar,45,"ad");
            cmd_kalite_ins.Parameters.Add("@oran1", MySqlDbType.Double, 11, "oran1");
            cmd_kalite_ins.Parameters.Add("@oran2", MySqlDbType.Double, 11, "oran2");
            cmd_kalite_ins.Parameters.Add("@oran3", MySqlDbType.Double, 11, "oran3");

            MySqlCommand cmd_kalite_upd = 
                new MySqlCommand("update kalite set ad=@ad,oran1=@oran1,oran2=@oran2,oran3=@oran3 where id=@id;", conn);
            cmd_kalite_upd.Parameters.Add("@id", MySqlDbType.Int32, 11, "id");
            cmd_kalite_upd.Parameters.Add("@ad", MySqlDbType.VarChar, 45, "ad");
            cmd_kalite_upd.Parameters.Add("@oran1", MySqlDbType.Double, 11, "oran1");
            cmd_kalite_upd.Parameters.Add("@oran2", MySqlDbType.Double, 11, "oran2");
            cmd_kalite_upd.Parameters.Add("@oran3", MySqlDbType.Double, 11, "oran3");

            da_kalite = new MySqlDataAdapter();
            da_kalite.SelectCommand = cmd_kalite_sel;
            da_kalite.InsertCommand = cmd_kalite_ins;
            da_kalite.UpdateCommand = cmd_kalite_upd;


            comboboxdoldur();

        }

        private void comboboxdoldur()
        {
            try
            {
                ckalite.Items.Clear();

                dt_kalite = new DataTable();
                da_kalite.Fill(dt_kalite);

                int say = dt_kalite.Rows.Count;
                for (int i = 0; i < say; i++)
                {
                    ckalite.Items.Add(dt_kalite.Rows[i]["ad"]);
                }
                ckalite.Text = "Seçiniz";

            }
            catch { 
            }

            listboxdoldur();
        }
        private void listboxdoldur()
        {
            lkalite.Items.Clear();
            lmiktar.Items.Clear();
            loran1.Items.Clear();
            loran2.Items.Clear();
            loran3.Items.Clear();

            try
            {
                DataTable dt_urun = new DataTable();
                da_urun.Fill(dt_urun);

                int say = dt_urun.Rows.Count;

                for (int i = 0; i < say; i++)
                {
                    int kaliteid = (int)dt_urun.Rows[i]["kaliteid"];
                    int miktar = (int)dt_urun.Rows[i]["miktar"];

                    DataRow dr = kalitegetir(kaliteid);

                    if (dr != null)
                    {
                        lkalite.Items.Add(dr["ad"]);
                        lmiktar.Items.Add(miktar);
                        loran1.Items.Add(miktar*(double)dr["oran1"]);
                        loran2.Items.Add(miktar * (double)dr["oran2"]);
                        loran3.Items.Add(miktar * (double)dr["oran3"]);

                    }


                }


            }
            catch
            {

            }
        }

        private DataRow kalitegetir(int id)
        {
            DataRow[] dr = dt_kalite.Select("id=" + id);
            if (dr.Length > 0)
                return dr[0];
            return null;
        }

        private void bkalite_Click(object sender, EventArgs e)
        {
            fkalite frm = new fkalite(da_kalite);
            frm.ShowDialog();
            comboboxdoldur();
        }

        private void bekle_Click(object sender, EventArgs e)
        {
            int secilen = ckalite.SelectedIndex;
            int miktar;
            bool k = Int32.TryParse(tmiktar.Text,out miktar);

            if (k && secilen > -1)
            {
                try
                {
                    DataTable dt_urun = new DataTable();
                    da_urun.Fill(dt_urun);

                    DataRow dr = dt_urun.NewRow();

                    dr["kaliteid"] = dt_kalite.Rows[secilen]["id"];
                    dr["miktar"] = miktar;
                    dt_urun.Rows.Add(dr);
                    da_urun.Update(dt_urun);

                    ckalite.Text = "Seçiniz";
                    tmiktar.Clear();
                }
                catch { 
                }
            }
            listboxdoldur();

        }
    }
}
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 MySql.Data.MySqlClient;


namespace recel
{
    public partial class fkalite : Form
    {
        MySqlDataAdapter da;
        DataTable dt;
        public fkalite(MySqlDataAdapter _da)
        {
            InitializeComponent();
            da = _da;
            datagrid();
        }

        private void datagrid()
        {
            try
            {
                dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;
                dataGridView1.AllowUserToDeleteRows = false; // silme islemi iptal
                dataGridView1.Columns["id"].Visible = false;
                dataGridView1.Columns["ad"].HeaderText = "Kalite Adı";
                dataGridView1.Columns["oran1"].HeaderText = "Meyve O.";
                dataGridView1.Columns["oran2"].HeaderText = "Şeker O.";
                dataGridView1.Columns["oran3"].HeaderText = "Su O.";

            }
            catch { 
            }
        }

        private void bkapat_Click(object sender, EventArgs e)
        {
            try
            {
                // dt ayrica (DataTable)dataGridView1.DataSource esittir 
                //da.Update((DataTable)dataGridView1.DataSource);

                da.Update(dt);
                this.Close();
            }
            catch { 
            }


        }
    }
}

NYP-2 2016 Hafta-11 Uygulamaları

Share