コード例 #1
0
        //When the user wants to search for a book
        private void btnSearchBook_Click(object sender, EventArgs e)
        {
            //Create a list of books to store result(s) in
            List <Book> bookSearchResult = new List <Book>();

            //pyb year and out of stock are empty
            String pubYear    = "";
            String outOfStock = "";

            //Initially set id to 0 (nothing entered in textbox)
            int ISBN = 0;

            //If ISBN text box has something in it, replace 0 with the ID input by user
            if (txtISBN.Text != "")
            {
                if (txtISBN.MaskCompleted)
                {
                    ISBN = Int32.Parse(txtID.Text);
                }
                else
                {
                    MessageBox.Show("Please enter a valid ISBN Code", "Invalid ISBN Code", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }

            //If the date time picker isn't empty, put the value into the variable
            if (dtpPubYear.Text != " ")
            {
                pubYear = dtpPubYear.Text;
            }


            //All of user input into variables
            String title    = txtTitle.Text;
            String lang     = cmbLang.Text;
            String category = cmbCategory.Text;

            //Sets the variable to true/false (as string for the SQL statement) based on whether the user wants to see items out of stock or in stock
            if (cmbOutOfStock.Text == "Yes")
            {
                outOfStock = "True";
            }
            else if (cmbOutOfStock.Text == "No")
            {
                outOfStock = "False";
            }

            //If there are no students to edit, disable the edit link
            if (bookSearchResult.Count == 0)
            {
                lblEditBook.Enabled = false;
            }


            //Create a new book with properties set
            Book searchBook = new Book(ISBN, title, lang, "", 0, 0, category, pubYear, outOfStock, 0, 0);

            //Create a connection to the database and search for the student
            LibraryDB book = new LibraryDB();

            //Search for the student
            bookSearchResult = book.BookSearch(searchBook);

            //Load the student data into the data grid view
            dgvBooks.DataSource = bookSearchResult;

            //Displays number of returned records to the user
            if (bookSearchResult.Count == 1)
            {
                lblNumBooks.Text = bookSearchResult.Count + " Book matches your filter.";
            }
            else
            {
                lblNumBooks.Text = bookSearchResult.Count + " Books match your filter.";
            }

            //Remove the out of stock column
            dgvBooks.Columns.RemoveAt(8);

            //Column headers
            dgvBooks.Columns[0].HeaderText = "ISBN";
            dgvBooks.Columns[1].HeaderText = "Book\nTitle";
            dgvBooks.Columns[2].HeaderText = "Language";
            dgvBooks.Columns[3].HeaderText = "Binding\nName";
            dgvBooks.Columns[4].HeaderText = "Original\nStock";
            dgvBooks.Columns[5].HeaderText = "Current\nStock";
            dgvBooks.Columns[6].HeaderText = "Category";
            dgvBooks.Columns[7].HeaderText = "Publication\nYear";
            dgvBooks.Columns[8].HeaderText = "Shelf";
            dgvBooks.Columns[9].HeaderText = "Floor";

            //Dynamically size the data grid view each time the user searches
            int width = 0;

            foreach (DataGridViewColumn col in dgvBooks.Columns)
            {
                width += col.Width;
            }
            width += dgvBooks.RowHeadersWidth;

            dgvBooks.ClientSize      = new Size(width + 2, dgvBooks.Height);
            dgvBooks.BackgroundColor = System.Drawing.SystemColors.Control;
        }