0

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

 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();但我仍然必须关闭应用程序并再次打开它。

4

2 回答 2

0

你可以数据绑定你的gridview吗?

myDataGrid.DataBind();

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

于 2012-07-30T02:10:10.027 回答
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 事件中的数据网格中

于 2012-07-30T08:14:57.570 回答