private void btnSave_Click(object sender, EventArgs e) { try { if (dgvBookList.Rows.Count > 0) { BookDT = new DataTable(); BookDT.Columns.Add("BookCode"); BookDT.Columns.Add("BookName"); BookDT.Columns.Add("Author"); BookDT.Columns.Add("ISBN"); BookDT.Columns.Add("Category"); foreach (DataGridViewRow row in dgvBookList.Rows) { DataRow dr = BookDT.NewRow(); dr["BookCode"] = row.Cells[1].Value.ToString(); dr["BookName"] = row.Cells[2].Value.ToString(); dr["Author"] = row.Cells[3].Value.ToString(); dr["ISBN"] = row.Cells[4].Value.ToString(); string category = row.Cells[5].Value.ToString(); dr["Category"] = Category_DAO.getCategoryID(category); BookDT.Rows.Add(dr); dr = null; } if (BookDT.Rows.Count > 0) { if (Book_DAO.SaveNewBook(BookDT) > 0) { MessageBox.Show("Save Successfully.", "Save"); dgvBookList.DataSource = null; } } } else { MessageBox.Show("No Data found to Save."); } }catch (Exception ex) { MessageBox.Show(ex.Message); } }
//Generate Book Code private void GenerateBookCode() { int Code = Book_DAO.getBookCount() + dgvBookList.Rows.Count; txtBookCode.Text = string.Format("BK-" + "{0:00000}", ++Code); }