我点击datagridview
按钮时尝试将数据重新加载到refresh
,但点击后数据未刷新。使用MySQL
数据库,当我启动程序时,我进入数据库管理器SQLYog
并手动在该行中进行更改。之后,我切换到我的应用程序,然后按下刷新按钮以执行应用程序工作但没有任何事情发生。我不知道我做错了什么,网格中的数据没有刷新......
这是我的代码:
class MainWindow : Form
{
private MySqlDataAdapter adapter;
private MySqlCommandBuilder cmd;
private BindingSource bs = new BindingSource();
private DataGridView dataGridView1;
private DataTable dt = new DataTable();
// Constructor
public MainWindow()
{
this.InitializeComponent();
}
// Get data
public void GetData(string sql)
{
using (var conn = new MySqlConnection(Properties.Settings.Default.connString))
{
try
{
conn.Open();
adapter = new MySqlDataAdapter(sql, conn);
cmd = new MySqlCommandBuilder(adapter);
adapter.Fill(dt);
bs.DataSource = dt;
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
}
}
}
// Load
private void MainWindow_Load(object sender, EventArgs e)
{
dataGridView1.DataSource = bs;
GetData("SELECT * FROM grupe_artikala");
}
// Reload click
private void reload_Click(object sender, EventArgs e)
{
GetData(adapter.SelectCommand.CommandText);
}
}
答案 0 :(得分:0)
试试这个:
class MainWindow : Form
{
private MySqlDataAdapter adapter;
private MySqlCommandBuilder cmd;
private BindingSource bs = new BindingSource();
private DataGridView dataGridView1;
private DataTable dt = new DataTable();
String sql = "SELECT * FROM grupe_artikala";
// Constructor
public MainWindow()
{
this.InitializeComponent();
}
// Get data
public void GetData(string sql)
{
using (var conn = new MySqlConnection(Properties.Settings.Default.connString))
{
try
{
conn.Open();
adapter = new MySqlDataAdapter(sql, conn);
cmd = new MySqlCommandBuilder(adapter);
adapter.Fill(dt);
bs.DataSource = dt;
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
}
}
}
// Load
private void MainWindow_Load(object sender, EventArgs e)
{
dataGridView1.DataSource = bs;
GetData(sql);
}
// Reload click
private void reload_Click(object sender, EventArgs e)
{
dataGridView1.DataSource = null;
dataGridView1.DataSource = bs;
GetData(sql);
datagridview1.update();
datagridview1.refresh();
}
}
答案 1 :(得分:-3)
你应该写这个。这可能会解决您的问题
class MainWindow : Form
{
private MySqlDataAdapter adapter;
private MySqlCommandBuilder cmd;
private BindingSource bs = new BindingSource();
private DataGridView dataGridView1;
private DataTable dt = new DataTable();
// Constructor
public MainWindow()
{
this.InitializeComponent();
}
// Get data
public void GetData(string sql)
{
using (var conn = new MySqlConnection(Properties.Settings.Default.connString))
{
try
{
conn.Open();
adapter = new MySqlDataAdapter(sql, conn);
cmd = new MySqlCommandBuilder(adapter);
adapter.Fill(dt);
bs.DataSource = dt;
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
}
}
}
// Load
private void MainWindow_Load(object sender, EventArgs e)
{
dataGridView1.DataSource = bs;
String sql = "SELECT * FROM grupe_artikala";
}
// Reload click
private void reload_Click(object sender, EventArgs e)
{
GetData(sql);
}
}