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

グリッドビューをデータバインドできますか?

myDataGrid.DataBind();

これにより、グリッド内のデータが強制的に更新されます

于 2012-07-30T02:10:10.027 に答える