2013-10-15 60 views
0

我製作了以下WinForm,其中用戶提供書籍詳細信息並創建書籍記錄。這本書記錄被添加到具有CheckBox DataGridView的自動選擇:一個被取消選中的行總是被插入到數據庫以及來自dataGridView的選定行

Windows Form

Books Added to form

現在,當我取消了一些書,然後單擊「插入數據庫」按鈕,插入所選圖書數據庫中,取消選擇的書籍之一總是與選定的書籍一起插入到數據庫中。這可能是任何一本被取消選擇的書。
喜歡這裏我取消了一些書:

deselected Books

現在,當我點擊「插入到數據庫」按鈕,未被選中的圖書將會從DataGridView被刪除,選擇一個將b插入到數據庫:

Books inserted to database along with one unselected book

立即插入到數據庫中的書籍是所選擇的書籍「123」,「345,」 678' 890與一個未選擇的書沿‘’。像這樣,一本未被選中的書會隨着所選書籍一起被插入到數據庫中。
我所編寫的代碼是:

public partial class CreateBookRecord : Form 
     {     
      DataTable addedBooks; 
      DataColumn bookId, bookName, author, noOfCopies, noOfCopiesAvailable; 
      DataGridViewCheckBoxColumn check;  
      public CreateBookRecord() 
      { 
       InitializeComponent();     
      } 

      private void CreateBookRecord_Load(object sender, EventArgs e) 
      { 
       check = new DataGridViewCheckBoxColumn(); //Create a checkbox column for DataGridView     
       addedBooks = new DataTable(); 
       bookId = new DataColumn("Book ID", typeof(string)); 
       bookName = new DataColumn("Book Name", typeof(string)); 
       author = new DataColumn("Author", typeof(string)); 
       noOfCopies = new DataColumn("No of Copies", typeof(int)); 
       noOfCopiesAvailable = new DataColumn("No of Copies Available", typeof(int)); 
       addedBooks.Columns.AddRange(new DataColumn[] { bookId, bookName, author, noOfCopies,noOfCopiesAvailable }); 

       dataGridViewAddedBooks.Columns.Add(check); //To make first column as checkBox column 
       dataGridViewAddedBooks.DataSource = addedBooks; 
       dataGridViewAddedBooks.Columns["No of Copies Available"].Width = 0; 
      } 

      private void buttonCreateBook_Click(object sender, EventArgs e) 
      { 
        try 
        { 
         addedBooks.Rows.Add(textBoxID.Text, textBoxBookName.Text, textBoxAuthorName.Text,Convert.ToInt32(textBoxNoOfCopies.Text),Convert.ToInt32(textBoxNoOfCopies.Text));        
        } 
        catch (Exception ex) 
        { 
         MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
        } 
       } 

      private void dataGridViewAddedBooks_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e) 
      { 
       DataGridViewCheckBoxCell checkcell = (DataGridViewCheckBoxCell)dataGridViewAddedBooks.Rows[e.RowIndex].Cells[0]; 
       checkcell.Value = true; 
      } 

      private void buttonInsertBooks_Click(object sender, EventArgs e) 
      {   
       foreach (DataGridViewRow row in dataGridViewAddedBooks.Rows) 
       { 
        DataGridViewCheckBoxCell checkcell = (DataGridViewCheckBoxCell)row.Cells[0]; 
        if (checkcell.Value == check.FalseValue) 
        { 
         dataGridViewAddedBooks.Rows.Remove(row); 
        } 
       } 
       SqlBulkCopy copy = new SqlBulkCopy(connection); 
       copy.DestinationTableName = "Book"; 
       try 
       { 
        connection.Open(); 
        copy.WriteToServer(addedBooks); 
       } 
       catch (Exception ex) 
       { 
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
       } 
       finally 
       { 
        connection.Close(); 
       }   
      } 

,我會很感激,如果有人可以幫助我解決這個問題。
謝謝!

回答

1

我發現了這個問題。它坐落在這裏的代碼:

foreach (DataGridViewRow row in dataGridViewAddedBooks.Rows) 
       { 
        DataGridViewCheckBoxCell checkcell = (DataGridViewCheckBoxCell)row.Cells[0]; 
        if (checkcell.Value == check.FalseValue) 
        { 
         dataGridViewAddedBooks.Rows.Remove(row); 
        } 
       } 

當我們循環的DataGridView並刪除未選中的行,就產生問題。
假設您在其中插入了三行並取消選中第一行和第二行。現在,當我們遍歷第一行時,我們發現一個未經檢查的行,它從DataGridView中刪除。 DataGridView將在行刪除時自行刷新。現在,第二行變成了第一行。但'行'計數器值現在將是1(開始時爲0),它將檢查第二行(第一行是開頭的第二行)。所以它不會從dataGridView中移除,即使沒有被檢查,它也會被更新到數據庫。

解決方案:

DataTable insertedBooks=new DataTable(); //We will use a new table to store all the cheched records 
      insertedBooks = addedBooks.Clone(); 
      DataGridViewCheckBoxCell checkcell; 
      foreach (DataGridViewRow row in dataGridViewAddedBooks.Rows) 
      {    
       checkcell = (DataGridViewCheckBoxCell)row.Cells[0]; 
       if (checkcell.Value.Equals(true)) 
       { 
        insertedBooks.Rows.Add(row.Cells[1].Value.ToString(),row.Cells[2].Value.ToString(),row.Cells[3].Value.ToString(),Convert.ToInt32(row.Cells[4].Value),Convert.ToInt32(row.Cells[5].Value)); //Add all the checked records to insertedBooks table. 
       }       
      }    
      SqlBulkCopy copy = new SqlBulkCopy(connection); 
      copy.DestinationTableName = "Book"; 
      connection.Open(); 
      copy.WriteToServer(insertedBooks); //Use insertedBooks table to copy records to database table. 
      addedBooks.Clear(); //Delete all data from addedBooks table, it also cleard the DataGridView. 
相關問題