如何在将记录添加到DB的唯一表后,在应用程序中刷新/更新DataGridView

时间:2011-03-29 15:50:08

标签: c# datagridview

添加按钮的代码:

        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");
            }

}

1 个答案:

答案 0 :(得分:0)

我认为你做得太早了。填写booksDataset1.Books后尝试设置数据源。您在数据可用之前就已经绑定了,我不知道它正在接收一个可以刷新的更改事件。

或者,如果已经设置了数据源,您可以调用yourDataGridview.Refresh()来强制解决问题。