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

namespace WindowsFormsApplication5
{
    public partial class frmbook : Form
    {
       
        public frmbook()
        {
            InitializeComponent();
        }

        private void bookBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.bookBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.libraryDataSet);

        }

        private void frmbook_Load(object sender, EventArgs e)
        {
            #region setstate
            //if stringedite willbe false,bookBindingNavigator not shown;
            if (WindowsFormsApplication5.Properties.Settings.Default.stringedit == false)
            {
                bookBindingNavigator.Visible = false;
                txbsearch.Visible = true;
                cbxsearch.Visible = true;
                btnsearch.Visible = true;
                lnlhelp.Visible = true;
            }
            else
            {
                bookBindingNavigator.Visible = true;
                txbsearch.Visible = false;
                cbxsearch.Visible = false;
                btnsearch.Visible = false;
                lnlhelp.Visible = false;
            }
            #endregion
          
            // TODO: This line of code loads data into the 'libraryDataSet.book' table. You can move, or remove it, as needed.
            this.bookTableAdapter.Fill(this.libraryDataSet.book);


        }

        private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e)
        {

        }

        private void bindingNavigatorDeleteItem_Click(object sender, EventArgs e)
        {

        }

        private void lnlhelp_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            String msg = "enter entire of sentence or part of sentence";
            MessageBox.Show(msg);
        }

        private void toolStripCbx_Click(object sender, EventArgs e)
        {
         
        }

        private void btnsearch_Click(object sender, EventArgs e)
        {
            #region custom search
            try
            {
                StringBuilder pb = new StringBuilder();
                pb.Append("@");
                pb.Append(cbxsearch.Text);
                String strcom = cbxsearch.Text;
                StringBuilder strb = new StringBuilder();
                strb.Append("SELECT * FROM [book] WHERE ([");
                strb.Append(strcom);
                strb.Append("] like '%'+ @");
                strb.Append(strcom);
                strb.Append("+'%')");
                SqlConnection con = new SqlConnection();
                con.ConnectionString = WindowsFormsApplication5.Properties.Settings.Default.libraryConnectionString.ToString();
                SqlCommand com = new SqlCommand();
                com.Connection =con ;
                com.CommandType = CommandType.Text;
                com.CommandText = strb.ToString();
                String par = txbsearch.Text;
                com.Parameters.AddWithValue(pb.ToString(), par);
                SqlDataAdapter da = new SqlDataAdapter(com);
                DataTable dt = new DataTable();
                da.Fill(dt);

                bookDataGridView.DataSource = dt;
                this.tableAdapterManager.UpdateAll(this.libraryDataSet);

            }
            catch { }

            #endregion
            #region sort
            try
            {

                switch (cbxsearch.Text)
                {
                    case "isbn":
                        bookDataGridView.Sort(isbnDataGridViewTextBoxColumn, ListSortDirection.Ascending);
                        break;
                    case "name":
                        bookDataGridView.Sort(nameDataGridViewTextBoxColumn, ListSortDirection.Ascending);

                        break;
                    case "publisher":
                        bookDataGridView.Sort(publisherDataGridViewTextBoxColumn, ListSortDirection.Ascending);
                        break;
                    case "author":
                        bookDataGridView.Sort(authorDataGridViewTextBoxColumn, ListSortDirection.Ascending);
                        break;
                    case "translator":
                        bookDataGridView.Sort(translatorDataGridViewTextBoxColumn, ListSortDirection.Ascending);
                        break;
                         case "number":
                        bookDataGridView.Sort(numberDataGridViewTextBoxColumn, ListSortDirection.Ascending);
                        break;
                         case "exist":
                        bookDataGridView.Sort(existDataGridViewTextBoxColumn, ListSortDirection.Ascending);
                        break;
                  default: bookDataGridView.Refresh();
                        break;

                }
              
              
           
            
            }
            catch (Exception exep)
            {
                MessageBox.Show(exep.ToString());



            }

         #endregion
          
        }

        private void toolStripCbx_TextChanged(object sender, EventArgs e)
        {
            #region sort
              try
            {

                switch (toolStripCbx.Text)
                {
                    case "isbn":
                        bookDataGridView.Sort(isbnDataGridViewTextBoxColumn, ListSortDirection.Ascending);
                        break;
                    case "name":
                        bookDataGridView.Sort(nameDataGridViewTextBoxColumn, ListSortDirection.Ascending);

                        break;
                    case "publisher":
                        bookDataGridView.Sort(publisherDataGridViewTextBoxColumn, ListSortDirection.Ascending);
                        break;
                    case "author":
                        bookDataGridView.Sort(authorDataGridViewTextBoxColumn, ListSortDirection.Ascending);
                        break;
                    case "translator":
                        bookDataGridView.Sort(translatorDataGridViewTextBoxColumn, ListSortDirection.Ascending);
                        break;
                         case "number":
                        bookDataGridView.Sort(numberDataGridViewTextBoxColumn, ListSortDirection.Ascending);
                        break;
                         case "exist":
                        bookDataGridView.Sort(existDataGridViewTextBoxColumn, ListSortDirection.Ascending);
                        break;
                  default: bookDataGridView.Refresh();
                        break;

                }
              
              
           
            
            }
            catch (Exception exep)
            {
                MessageBox.Show(exep.ToString());



            }

         #endregion
        }
    }
}
