(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!