using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ayça_kahraman
{
    public partial class Form5 : Form
    {
        public Form1 frm1;
        public Form5()
        {
            InitializeComponent();
        }
        public OleDbConnection bag = new OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=data.mdb");
        public OleDbCommand kmt = new OleDbCommand();
        public OleDbDataAdapter adtr = new OleDbDataAdapter();
        public DataSet dtst = new DataSet();

        public void listele()
        {
            bag.Open();
            OleDbDataAdapter adtr = new OleDbDataAdapter("select * From musteri ", bag);
            adtr.Fill(dtst, "musteri");
            dataGridView1.DataSource = dtst.Tables["musteri"];
            adtr.Dispose();
            bag.Close();
        }
        private void Form5_Load(object sender, EventArgs e)
        {
            dtst.Clear();
            listele();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string silinecek;
            try
            {
                int row = 0;
                for (row = 0; row < dataGridView1.Rows.Count; row++)
                {
                    if (dataGridView1.Rows[row].Cells[0].Selected == true)
                    {
                        break;
                    }
                }
                silinecek = dataGridView1.Rows[row].Cells[0].Value.ToString();
                DialogResult cevap;
                cevap = MessageBox.Show("Kaydı silmek istediğinizden eminmisiniz", "Uyarı", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (cevap == DialogResult.Yes)
                {
                   bag.Open();
                    kmt.Connection = bag;
                   kmt.CommandText = "DELETE from musteri WHERE Kodu='" + silinecek + "'";
                 kmt.ExecuteNonQuery();
                    kmt.Dispose();
                    bag.Close();
                   dtst.Clear();
                    dtst.Tables["musteri"].Clear();
                    listele();
                }
            }
            catch
            {
                ;
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            OleDbDataAdapter adtr = new OleDbDataAdapter("select * From musteri", bag);
            if (textBox1.Text == "")
            {
               kmt.Connection = bag;
                kmt.CommandText = "Select * from musteri";
                adtr.SelectCommand = kmt;
                adtr.Fill(dtst, "musteri");
            }
            if (Convert.ToBoolean(bag.State) == false)
            {
               bag.Open();
            }
            adtr.SelectCommand.CommandText = " Select * From musteri" +
                 " where(Kodu like '%" + textBox1.Text + "%' )";
           dtst.Tables["musteri"].Clear();
            adtr.Fill(dtst, "musteri");
            bag.Close();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            OleDbDataAdapter adtr = new OleDbDataAdapter("select * From musteri", bag);
            if (textBox2.Text == "")
            {
                kmt.Connection = bag;
                kmt.CommandText = "Select * from musteri";
                adtr.SelectCommand = kmt;
                adtr.Fill(dtst, "musteri");
            }
            if (Convert.ToBoolean(bag.State) == false)
            {
                bag.Open();
            }
            adtr.SelectCommand.CommandText = " Select * From musteri" +
                 " where(Adi like '%" + textBox2.Text + "%' )";
            dtst.Tables["musteri"].Clear();
            adtr.Fill(dtst, "musteri");
            bag.Close();
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            OleDbDataAdapter adtr = new OleDbDataAdapter("select * From musteri", bag);
            if (textBox3.Text == "")
            {
                kmt.Connection = bag;
                kmt.CommandText = "Select * from musteri";
                adtr.SelectCommand = kmt;
                adtr.Fill(dtst, "musteri");
            }
            if (Convert.ToBoolean(bag.State) == false)
            {
                bag.Open();
            }
            adtr.SelectCommand.CommandText = " Select * From musteri" +
                 " where(Telefonu like '%" + textBox3.Text + "%' )";
            dtst.Tables["musteri"].Clear();
            adtr.Fill(dtst, "musteri");
            bag.Close();
        }
    }
}
