本文介绍了一个取消选择的行总是与dataGridView中的选定行一起插入到数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 (点击链接查看图片了解) 我制作了以下WinForm,其中用户提供图书详细信息并创建图书记录。这本书记录被添加到dataGridView并自动选中了checkBox: http://i.stack.imgur.com/VOOTL.png [ ^ ] http ://i.stack.imgur.com/MpYNM.png [ ^ ] 现在,当我取消选择一些书籍并点击插入数据库按钮将所选书籍插入数据库时,其中一个取消选择的书籍总是与所选书籍一起插入数据库。这可能是任何一本取消选择的书。 像这里我取消选择一些书: http://i.stack.imgur.com/MM4eM.png [ ^ ] 现在点击插入数据库按钮,未选择的书籍将从dataGridView中删除,所选的书籍将被插入数据库: http://i.stack.imgur.com/TE1gJ.png [ ^ ] 现在插入数据库的书籍是选定的书籍123','345,'678'以及一本未经选择的书'890'。像这样,一本未经选择的书总是与所选书籍一起插入数据库。 我写的代码是: public partial class CreateBookRecord:Form { SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings [ myConnectionString]。ConnectionString); DataTable addedBooks; DataColumn bookId,bookName,author,noOfCopies,noOfCopiesAvailable; DataGridViewCheckBoxColumn检查; public CreateBookRecord() { InitializeComponent(); } private void CreateBookRecord_Load( object sender,EventArgs e) { check = new DataGridViewCheckBoxColumn(); check.TrueValue = true ; check.FalseValue = false ; addedBooks = new DataTable(); bookId = new DataColumn( 图书ID , typeof ( string )); bookName = new DataColumn( 图书名称, typeof ( string )); author = new DataColumn( 作者 , typeof ( string )); noOfCopies = new DataColumn( No of of复制, typeof ( int )); noOfCopiesAvailable = new DataColumn( No of of副本可用, typeof ( int )); addedBooks.Columns.AddRange( new DataColumn [] {bookId,bookName,author,noOfCopies,noOfCopiesAvailable}); dataGridViewAddedBooks.Columns.Add(check); dataGridViewAddedBooks.DataSource = addedBooks; dataGridViewAddedBooks.Columns [ 没有可用的副本]。宽度= 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, 错误,MessageBoxButtons.OK,MessageBoxIcon.Error); } } private void dataGridViewAddedBooks_RowsAdded( object sender,DataGridViewRowsAddedEventArgs e) { DataGridViewCheckBoxCell checkcell =(DataGridViewCheckBoxCell)dataGridViewAddedBooks.Rows [e.RowIndex] .Cells [ 0 ]; checkcell.Value = check.TrueValue; } private void dataGridViewAddedBooks_CellContentClick( object sender,DataGridViewCellEventArgs e) { if (e.ColumnIndex == 0 ) { DataGridViewCheckBoxCell checkcell; checkcell =(DataGridViewCheckBoxCell)dataGridViewAddedBooks.Rows [e.RowIndex] .Cells [e.ColumnIndex]; if (checkcell.Value == check.TrueValue) { dataGridViewAddedBooks.Rows [e.RowIndex] .DefaultCellStyle.BackColor = Color.Blue; checkcell.Value = check.FalseValue; } else if (checkcell.Value == check.FalseValue) { dataGridViewAddedBooks.Rows [e.RowIndex] .DefaultCellStyle.BackColor = Color.White; checkcell.Value = check.TrueValue; } } else { MessageBox.Show( 您选择了无效的单元格, 无效选择,MessageBoxButtons.OK,MessageBoxIcon.Error); } } 私有 void buttonInsertBooks_Click( object sender,EventArgs e) { foreach (DataGridViewRow row dataGridViewAddedBooks.Rows) { DataGridViewCheckBoxCell checkcell =(DataGridViewCheckBoxCell)row.Cells [ 0 ]。 if (checkcell.Value == check.FalseValue) { dataGridViewAddedBooks.Rows.Remove(row); } } SqlBulkCopy copy = new SqlBulkCopy(连接); copy.DestinationTableName = Book; 尝试 { connection.Open(); copy.WriteToServer(addedBooks); } catch (Exception ex) { MessageBox.Show(ex.Message, 错误,MessageBoxButtons.OK,MessageBoxIcon.Error); } 最后 { connection.Close(); } } 如果有人能帮我解决这个问题,我将非常感激。 谢谢!解决方案 Ashish ....你有很好的编程技巧......我喜欢你关心项目的GUI。 /> 让我们来解决问题.... 你在这里编写的代码部分有点复杂到明白.. 但是......我仍然会尽力帮助你... 我认为你在'图书选择过程'完成后正在更新DataGridView。 这是你的解决方案.... 尝试...确保您只传递那些选中了CheckBoxes的记录 使用'调试'模式验证哪些确切的值传递'插入数据库'事件。 快乐编程....... : - ) (Check out the pics to understand by clicking the link)I have made the following WinForm in which the user provide book details and create a book record. This book record gets added to the dataGridView with the checkBox automatically selected:http://i.stack.imgur.com/VOOTL.png[^]http://i.stack.imgur.com/MpYNM.png[^]Now, when I deselect some books and click on 'Insert to database' button to insert the selected books to database, one of the deselected books always gets inserted to database along with the selected ones. And that may be any one of the deselected books.Like here I deselect some books:http://i.stack.imgur.com/MM4eM.png[^]Now when I click the 'Insert to Database' button, the unselected books will be deleted from dataGridView and the selected one will b inserted to database:http://i.stack.imgur.com/TE1gJ.png[^]Now the books inserted to database are the selected books '123','345,'678' along with one unselected book '890'. Like this, one unselected book always get inserted to database along with the selected books.The code which I have written is:public partial class CreateBookRecord : Form { SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString); DataTable addedBooks; DataColumn bookId, bookName, author, noOfCopies, noOfCopiesAvailable; DataGridViewCheckBoxColumn check; public CreateBookRecord() { InitializeComponent(); } private void CreateBookRecord_Load(object sender, EventArgs e) { check = new DataGridViewCheckBoxColumn(); check.TrueValue = true; check.FalseValue = false; 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); 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 = check.TrueValue; } private void dataGridViewAddedBooks_CellContentClick(object sender, DataGridViewCellEventArgs e) { if (e.ColumnIndex == 0) { DataGridViewCheckBoxCell checkcell; checkcell = (DataGridViewCheckBoxCell)dataGridViewAddedBooks.Rows[e.RowIndex].Cells[e.ColumnIndex]; if (checkcell.Value == check.TrueValue) { dataGridViewAddedBooks.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Blue; checkcell.Value = check.FalseValue; } else if (checkcell.Value == check.FalseValue) { dataGridViewAddedBooks.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.White; checkcell.Value = check.TrueValue; } } else { MessageBox.Show("You selected an invalid cell", "Invalid Selection", MessageBoxButtons.OK, MessageBoxIcon.Error); } } 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(); } }I would be very thankful if anyone could help me resolve this problem.Thank you! 解决方案 Ashish ....You have a Good Programming Skills...I like your GUI for your concern project.lets come to problem....The part of coding stuff you gave here is somehow complicated to understand..But...still I will try to help you...I think you are Updating a DataGridView after 'Book Selection Process' is completed.Here is your solution....Try...to make sure that You are passing ONLY those Records which has Selected CheckBoxesUse 'Debugging' mode to verify which exact values are passing on 'Insert to Database' event.Happy Programming.......:-) 这篇关于一个取消选择的行总是与dataGridView中的选定行一起插入到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-02 01:41