通过按钮单击刷新C#中的DataGridView

时间:2015-06-22 10:21:11

标签: c# datagridview refresh add

一段时间以来,我一直试图弄清楚如何通过按钮点击刷新C#中的datagridview,因为目前我不得不关闭并重新打开程序以使其更新。我希望这样一旦用户添加数据或删除数据,就会刷新/更新datagridview以显示更改。数据存储在MS Access数据库中。我尝试过使用:

datagridview.Refresh();
datagridview.Update();

但它没有改变任何东西。

以下是用户添加新“播放器”以及用户删除“播放器”时使用的代码

private void btnAdd_Click(object sender, EventArgs e)
    {
        //This code uses the connection that was at the beginning of the form. It opens a connection to the MS Access database.
        connect.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\Users\Aaron\Documents\Cardfight Vanguard Tournament Program\Tournament\Tournament\Players.mdb";
        connect.Open();

        //This code creates a new command but at the same time tells the application where the data the user has inserted will be going.
        OleDbCommand cmd = new OleDbCommand("INSERT INTO Players VALUES (FirstName, LastName)", connect);

        //This blocks defines where the data will be coming from so the text box for the ISBN number is going too isbn.
        if (connect.State == ConnectionState.Open)
        {
            cmd.Parameters.Add("FirstName", OleDbType.VarChar, 20).Value = txtFirstName.Text;
            cmd.Parameters.Add("LastName", OleDbType.VarChar, 20).Value = txtLastName.Text;

            try
            {
                //Once that data has been stored a popup box will appear telling the user that it has been saved.
                cmd.ExecuteNonQuery();
                MessageBox.Show("Data Added", "Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);

                txtFirstName.Clear();
                txtLastName.Clear();

                gridViewPlayers.Update();
                gridViewPlayers.Refresh();

                //This closes the connection to the MS Access database.
                connect.Close();

            }
            catch (Exception expe)
            {
                MessageBox.Show(expe.Source);
                connect.Close();
            }
        }
        else
        {
            //If for some reason the connection to the database should fail, a different pop up will appear telling the user it has failed.
            //It will then close the connection.
            MessageBox.Show("Connection Failed");
        }

        connect.Close();
        this.playersTableAdapter.Fill(this.playersDataSet.Players);

    }

    private void btnDelete_Click(object sender, EventArgs e)
    {
        connect.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\Users\Aaron\Documents\Cardfight Vanguard Tournament Program\Tournament\Tournament\Players.mdb";

        //This is where the string from the beginning of the application is called. It is used to temporarily store the data that is in txtFirstName.
        tempFirstName = txtFirstName.Text;

        connect.Open();

        //And then this command defines what row of data should be deleted.
        OleDbCommand cmd = new OleDbCommand("DELETE FROM Players WHERE FirstName ='" +tempFirstName+ "'", connect);

        cmd.ExecuteNonQuery();
        MessageBox.Show("Data Deleted", "Deleted", MessageBoxButtons.OK, MessageBoxIcon.Information);

        txtFirstName.Clear();
        txtLastName.Clear();

        connect.Close();
    }

0 个答案:

没有答案