刷新网格视图而无需再次打开应用程序

时间:2012-07-30 02:06:47

标签: c# c#-4.0 ms-access ado.net

有没有办法在不关闭应用程序并再次打开它的情况下刷新网格视图?我试图以这种方式刷新网格视图。

 private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.Parent.Refresh();

        }

我以这种方式插入 Access数据库

private void button2_Click(object sender, EventArgs e)
        {
            //Setting up Connection String
            string connectionString1 = GetConnectionString();
            OleDbConnection myConnection1 = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source= C:\\Users\\Daffodils\\Documents\\WindowsFormsApplication11\\WindowsFormsApplication11\\WindowsFormsApplication11\\PersonDatabase.mdb");
            //OleDbConnection myConnection1 = new OleDbConnection(connectionString1);
            String insertString = "Insert Into PersonTable([FirstName],[LastName],[City],[Age]) Values ('" + "John" + "','" + "Gray" + "','" + "Toronto" + "','" + "50" + "')";

            using (myConnection1)
            {
                OleDbCommand command = myConnection1.CreateCommand();
                command.CommandText = insertString;

                try
                {
                    // openning a connection to the database / table
                    myConnection1.Open();

                    //// SQL commnd class
                    OleDbDataReader myDataReader1 = command.ExecuteReader(); // exists as a part of SQL command class

                    //Closing Database connection
                    myDataReader1.Close();
                    //Console.WriteLine("Data was added to the table !!!");
                    MessageBox.Show("Data was added to the table !!!");
                }
                // dealing with exceptions
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    //Console.WriteLine(ex.Message); // printing exception message to default output
                }
            }
        }

我尝试过使用dataGridView1.EndEdit();,但我仍然需要关闭应用程序并再次打开它。

2 个答案:

答案 0 :(得分:0)

你可以数据化你的gridview吗?

myDataGrid.DataBind();

这将强制刷新网格中的数据

答案 1 :(得分:0)

让我们假设你的button1是刷新。你可以尝试这种技术。

private void button1_Click(object sender, EventArgs e)
    {
        //code for loading again the data to the grid
    }

然后在MessageBox.Show("Data was added to the table !!!");之后  把这段代码

button1.PerformClick();

因此,您需要将代码放在如何将数据加载到button1事件

中的数据网格中