c#数据到datagridview不会重新加载

本文关键字:新加载 加载 数据 datagridview | 更新日期: 2023-09-27 18:05:24

当我点击refresh按钮时,我试图重新加载数据到datagridview,但点击后数据未刷新。我使用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);
    }
}

c#数据到datagridview不会重新加载

试试这个:

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

你应该这样写。这可能会解决您的问题

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