简体   繁体   中英

How to refresh/update DataGridView in an application after adding a record to the DB's only table

Code for the add button:

        private void addRecord()
        {
            int copies;
            if (txtBookCode.Text != "" && txtBookTitle.Text != "" && txtBookCategory.Text != "" && txtBookCopies.Text != "")
            {
                if (int.TryParse(txtBookCopies.Text, out copies))
                {
                    //DataRow row = booksDataset1.Tables["Books"].NewRow();

                    //row[1] = txtBookTitle.Text;
                    //row[2] = txtBookCategory.Text;
                    //row[3] = copies;
                    //row[4] = txtBookCode.Text;

                    //booksDataset1.Tables["Books"].Rows.Add(row);

                    //sqlDataAdapter1.Update(booksDataset1, "Books");

                    SqlCommand addRecord = new SqlCommand();
                    addRecord.Connection = BooksConnection;
                    addRecord.CommandType = CommandType.Text;
                    addRecord.CommandText =
                        "INSERT INTO Books VALUES (@BookCode, @BookTitle, @BookCategory, @BookCopies)";

                    SqlParameter BookCodeParameter = new SqlParameter();
                    BookCodeParameter.ParameterName = "@BookCode";
                    BookCodeParameter.SqlDbType = SqlDbType.Int;
                    BookCodeParameter.IsNullable = true;
                    BookCodeParameter.Value = txtBookCode.Text;

                    SqlParameter BookTitleParameter = new SqlParameter();
                    BookTitleParameter.ParameterName = "@BookTitle";
                    BookTitleParameter.SqlDbType = SqlDbType.NVarChar;
                    BookTitleParameter.IsNullable = true;
                    BookTitleParameter.Value = txtBookTitle.Text;

                    SqlParameter BookCategoryParameter = new SqlParameter();
                    BookCategoryParameter.ParameterName = "@BookCategory";
                    BookCategoryParameter.SqlDbType = SqlDbType.NVarChar;
                    BookCategoryParameter.IsNullable = true;
                    BookCategoryParameter.Value = txtBookCategory.Text;

                    SqlParameter BookCopiesParameter = new SqlParameter();
                    BookCopiesParameter.ParameterName = "@BookCopies";
                    BookCopiesParameter.SqlDbType = SqlDbType.Int;
                    BookCopiesParameter.IsNullable = true;
                    BookCopiesParameter.Value = txtBookCopies.Text;

                    addRecord.Parameters.Add(BookCodeParameter);
                    addRecord.Parameters.Add(BookTitleParameter);
                    addRecord.Parameters.Add(BookCategoryParameter);
                    addRecord.Parameters.Add(BookCopiesParameter);

                    addRecord.Connection.Open();

                    addRecord.ExecuteNonQuery();

                    clearTextBoxes();
//guess attemp at refreshing the datagridview
                    BooksGrid.DataSource = null;

                    BooksGrid.DataSource = booksDataset1.Books;

                    sqlDataAdapter1.Fill(booksDataset1.Books);
                }
                else
                    MessageBox.Show("Please enter a number into \'Copies\' field");
            }
            else
            {
                MessageBox.Show("Please fill all of the fields");
            }

}

I think you're doing it too early. Try setting the datasource after you've filled booksDataset1.Books. You're binding before data is available and I don't know that it's picking up a change event that would make it refresh.

Alternately, you can call yourDataGridview.Refresh() to force the issue if the Datasource is already set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM