我制作了以下WinForm,其中用户提供书籍详细信息并创建书籍记录。这本书记录被添加到dataGridView中,并自动选中了复选框:
现在,当我取消选择某些书籍并点击“插入数据库”按钮将所选书籍插入数据库时,其中一个取消选择的书籍将始终与所选书籍一起插入数据库。这可能是任何一本取消选中的书。
就像在这里我取消选择一些书:
现在,当我点击“插入数据库”按钮时,将从dataGridView中删除未选择的书籍,并将所选书籍插入数据库:
现在插入数据库的书籍是选定的书籍'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();
}
}
如果有人能帮我解决这个问题,我将非常感激。
谢谢!
答案 0 :(得分: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.