尝试为gridview创建刷新按钮

时间:2013-12-05 11:09:30

标签: c# database visual-studio-2010 gridview datagridview

我已经创建了一个c#程序来访问数据库并向其写入一些数据,并在网格视图中显示它。 这一切都有效,但现在我希望网格视图刷新,因为它不会显示我刚刚输入数据库的数据

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;


namespace ForexDev
{
    public partial class Form1 : Form
    {
        private OleDbConnection Database1;
        private OleDbCommand oledbcmd = new OleDbCommand();
        private string connParam = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\floortje\Documents\Visual Studio 2010\Projects\ForexDev\ForexDev\Database11.accdb;Persist Security Info=False";

        public Form1()
        {
            Database1 = new OleDbConnection(connParam);
            InitializeComponent();

        }

        private void btnsave_Click(object sender, EventArgs e)
        {
            try
            {

                Database1.Open();
                oledbcmd.Connection = Database1;
                oledbcmd.CommandText = "INSERT INTO Forex ([Order],[Tijd gekocht],Type,Groote,Symbool,[Koers inkoop],[S/l],[T/p],[Koers Verkoop],[Profit/Loss]) VALUES ('" + this.txtOrder.Text + "','" + this.txtTijd.Text + "','" + this.txtType.Text + "','" + this.txtgroote.Text + "','" + this.txtSymb.Text + "','" + this.txtKoop.Text + "','" + this.StopLoss.Text + "','" + this.TakeProfit.Text + "','" + this.txtVerkoop.Text + "','" + this.Winstverl.Text + "');";
                oledbcmd.CommandType = CommandType.Text;
                int temp = oledbcmd.ExecuteNonQuery();
                dataGridView1.Refresh();
                dataGridView1.Update();
                Database1.Close();


                if (temp > 0)
                {
                    MessageBox.Show("Added");

                }
                else
                {
                    MessageBox.Show("Failed");
                }


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'database11DataSet.Forex' table. You can move, or remove it, as needed.
            this.forexTableAdapter.Fill(this.database11DataSet.Forex);



        }

        private void button2_Click(object sender, EventArgs e)
        {
            Database1.Close();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Database1.Open();

            this.dataGridView1.Refresh();
            this.dataGridView1.Update();
            Database1.Close();
        }

    }

}

2 个答案:

答案 0 :(得分:1)

我想感谢tariq让我走上正轨,因为以下原因,我没有将你的答案标记为我的问题的答案:

我的问题的答案是我需要将数据库绑定到gridview,如下所示: (如果这不是数据绑定,请纠正我)

{

                    Database1.Open();
                    oledbcmd.Connection = Database1;
                    oledbcmd.CommandText = textBox1.Text;
                    oledbcmd.CommandText = "DELETE FROM Forex ([Order],[Tijd gekocht],Type,Groote,Symbool,[Koers inkoop],[S/l],[T/p],[Koers Verkoop],[Profit/Loss]) VALUES ('" + this.txtOrder.Text + "','" + this.txtTijd.Text + "','" + this.txtType.Text + "','" + this.txtgroote.Text + "','" + this.txtSymb.Text + "','" + this.txtKoop.Text + "','" + this.StopLoss.Text + "','" + this.TakeProfit.Text + "','" + this.txtVerkoop.Text + "','" + this.Winstverl.Text + "');";
                    oledbcmd.CommandType = CommandType.Text;
                    int temp = oledbcmd.ExecuteNonQuery();
                    dataGridView1.DataSource = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\floortje\Documents\Visual Studio 2010\Projects\ForexDev\ForexDev\Database11.accdb;Persist Security Info=False";
                    dataGridView1.Refresh();
                    this.dataGridView1.Refresh();
                    DataSet ds = new DataSet();
                    DataTable dt = new DataTable();
                    ds.Tables.Add(dt);
                    OleDbDataAdapter dd = new OleDbDataAdapter();
                    dd = new OleDbDataAdapter("Select * From Forex", Database1);
                    dd.Fill(dt);
                    dataGridView1.DataSource = dt.DefaultView;

                    Database1.Close();

答案 1 :(得分:0)

在进行更新后添加此内容

dataGridView1.DataBind();