更新时如何刷新数据网格视图? C#窗口

我想在用户向数据库插入一些项目后刷新数据网格视图。 我试图清空视图的数据源并为其应用新的数据源,但它不起作用。

这是系统的流程:

  • 点击添加项目,然后弹出add-item form
  • 输入产品详情,然后点击按钮插入数据
  • 查看更新<-----问题
  • 在这里输入图像描述

    在这一部分,将有two forms和一个database claee 。 它们是Form1 (数据视图), Add-item formdatabase_function.cs

    这是我在database class上的代码:

      public class database_function
    {
       OleDbConnection connect = new OleDbConnection();
    
    
        public database_function()
        {
            connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:sys_db.accdb";
        }
    
        public void database_connect(String item_code, String des, String unit, double price)
        {
          Form1 f1 = new Form1();
            try
            {
    
                connect.Open();
                OleDbCommand command = new OleDbCommand();
                command.Connection = connect;
                command.CommandText = "INSERT into item (item_code, description, unit, price) values ('" + item_code + "', '" + des + "', '" + unit + "', " + price + ")";
                command.ExecuteNonQuery();
    
                connect.Close();
            }
            catch(Exception e)
            {
                Debug.WriteLine(e.Source);
                connect.Close();
            }
           f1.refresh_dataGridView();
        }
    
    
    
        //return the dataGridView to form 1, and show the database data on it.
        public DataTable get_view()
        {
            connect.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = connect;
            string query = "Select item_code, description, unit, price from item";
            command.CommandText = query;
    
            OleDbDataAdapter da = new OleDbDataAdapter(command);
            DataTable dt = new DataTable();
            da.Fill(dt);
            connect.Close();
            Debug.WriteLine("return dataTable");
            return dt;
        }
    }
    

    下面是form1上的代码://当form1加载时获取数据源private void Form1_Load(object sender,EventArgs e){dataGridView1.DataSource = df.get_view();
    }

       //Refresh the view
       public void refresh_dataGridView()
        {
            dataGridView1.DataSource = null;
            dataGridView1.DataSource = df.get_view();
            Debug.WriteLine("refuesh");
        }
    

    这是add-item. form add-item. form

    private void button1_Click(object sender, EventArgs e)
        {
           //Get textfield data....
    
            //If user doesn't input the item data, show the error message. Else, update to database.
            if (error == true)
            {
                error_msg_form emf = new error_msg_form();
                emf.Show();
            }
            else
            {
                Form1 f1 = new Form1();
                database_function df = new database_function();
                 df.database_connect(item_code_tb.Text, des_tb.Text, unit_tb.Text, Convert.ToDouble(unit_price_tb.Text));
                //f1.refresh_dataGridView();
            }
        }
    

    但是,我调试日志会显示refresh ,这意味着它可以在refresh_dataGridView()运行。 但它无法刷新视图。

    问题是什么? 或者有另一种方法可以做到这一点? 谢谢


    当调用database_connect方法,通过形式的参考,如下面this

    private void button1_Click(object sender, EventArgs e)
    {
       //Get textfield data....
    
        //If user doesn't input the item data, show the error message. Else, update to database.
        if (error == true)
        {
            error_msg_form emf = new error_msg_form();
            emf.Show();
        }
        else
        {
            //Form1 f1 = new Form1();
            database_function df = new database_function();
             df.database_connect(this, item_code_tb.Text, des_tb.Text, unit_tb.Text, Convert.ToDouble(unit_price_tb.Text));
            //f1.refresh_dataGridView();
        }
    }
    

    然后,在下面的方法中添加参数Form1 form1 ,并使用相同的引用调用form1.refresh_dataGridView()

    public void database_connect(Form1 form1, String item_code, String des, String unit, double price)
        {
          //Form1 f1 = new Form1();
            try
            {
    
                connect.Open();
                OleDbCommand command = new OleDbCommand();
                command.Connection = connect;
                command.CommandText = "INSERT into item (item_code, description, unit, price) values ('" + item_code + "', '" + des + "', '" + unit + "', " + price + ")";
                command.ExecuteNonQuery();
    
                connect.Close();
            }
            catch(Exception e)
            {
                Debug.WriteLine(e.Source);
                connect.Close();
            }
           form1.refresh_dataGridView();
        }
    

    另外,如下所示更改代码以刷新数据网格视图,

    public void refresh_dataGridView()
        {
            dataGridView1.DataSource = typeof(List);
            dataGridView1.DataSource = df.get_view();
            Debug.WriteLine("refuesh");
        }
    

    您最好重构您的代码并将数据库访问代码分离为单独的类文件。 那么你可以从其他类,表单等调用这些函数。

    在插入项目方法之后的窗体中,您可以设置gridview数据源,例如:

    database_function df = new database_function();
    df.InsertItem("sdfsdf", "Description", "KG", 5);
    //load data again
    dataGridView1.DataSource = df.get_view();
    

    你需要两个方法,一个用于插入,另一个用于获取细节

    public class database_function
    {
         public void InsertItem(String item_code, String des, String unit, double price)
            {
               //Form1 f1 = new Form1();
                try
                {
                    connect.Open();
                    OleDbCommand command = new OleDbCommand();
                    command.Connection = connect;
                    command.CommandText = "INSERT into item (item_code, description, unit, price) values ('" + item_code + "', '" + des + "', '" + unit + "', " + price + ")";
                    command.ExecuteNonQuery();
    
                    connect.Close();
                }
                catch(Exception e)
                {
                    Debug.WriteLine(e.Source);
                    connect.Close();
                }
             //f1.refresh_dataGridView();
            }
    
     public DataTable get_view()
        {
            connect.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = connect;
            string query = "Select item_code, description, unit, price from item";
            command.CommandText = query;
    
            OleDbDataAdapter da = new OleDbDataAdapter(command);
            DataTable dt = new DataTable();
            da.Fill(dt);
            connect.Close();
            return dt;
        }
    

    }


    这是我用于我的代码:

        private void Form1_Activated(object sender, EventArgs e) {
            if (GlobalVariables.bReload == false) {
                Activated -= Form1_Activated;
                GlobalVariables.bReload = true;
                dgv1.DataSource = "my datasource"
                dgv1.Refresh();
                Activated += Form1_Activated;
            }
        }
    

    在我的情况下,我想在我对子窗体进行修改并返回到我的datagridview所在的主窗体后触发更新。 我使用了一个布尔类型的全局变量来管理它。

    链接地址: http://www.djcxy.com/p/71167.html

    上一篇: How to refresh the data grid view when update? Window from C#

    下一篇: How to handle the CheckBox in MouseClick events?