0

我的应用程序有一个通过 mysql 连接器显示数据的 datagridview,我想清除或刷新数据网格,但不知道如何。

我试过了

dataGridView2.DataSource = null; 

但它不会清除数据源。

这是我的代码

using MySql.Data.MySqlClient; 

Boolean DataLoaded = false;
    string SQL = null;

    MySqlConnection MysqlConn;
    MySqlCommand WerkDataCommand = new MySqlCommand();
    MySqlDataAdapter WerkDataAdapter = new MySqlDataAdapter();
    DataTable WerkData = new DataTable();

private void gegevensOphalenBTN_Click(object sender, EventArgs e)
    {
        MysqlConn = new MySqlConnection();

        MysqlConn.ConnectionString = "server=myserver;" + "user id=user;" + "password=***;" + "database=database";

        SQL = "SELECT * FROM werkverslagen";


        try
        {
            dataGridView2.DataSource = null;
            MysqlConn.Open();

            WerkDataCommand.Connection = MysqlConn;
            WerkDataCommand.CommandText = SQL; 

            WerkDataAdapter.SelectCommand = WerkDataCommand;
            WerkDataAdapter.Fill(WerkData);

            dataGridView2.DataSource = WerkData;
        }
        catch (MySqlException myerror)
        {
            MessageBox.Show("Database gives the following error: " + myerror.Message);
        }
        finally
        {
            MysqlConn.Close();
            MysqlConn.Dispose();
            button4.Enabled = true;
        }
    }

它仍将保留数据源并将相同的行添加到网格中。

4

1 回答 1

-1

你只需要这样做:

dataGridView2.Rows.Clear();
dataGridView2.Refresh();
于 2012-10-15T11:48:40.870 回答