我的应用程序有一个通过 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;
}
}
它仍将保留数据源并将相同的行添加到网格中。