Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 正在尝试为gridview创建刷新按钮_C#_Database_Visual Studio 2010_Gridview_Datagridview - Fatal编程技术网

C# 正在尝试为gridview创建刷新按钮

C# 正在尝试为gridview创建刷新按钮,c#,database,visual-studio-2010,gridview,datagridview,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 Syste

我制作了一个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();
        }

    }

}

在进行更新后添加此项

dataGridView1.DataBind();

我要感谢塔里克让我走上正轨,我没有将你的答案标记为我问题的答案,原因如下:

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


那么您面临的错误是什么?没有错误,但不会刷新。所以我有一个网格视图,我可以向其中写入数据,但新输入的数据不会显示。你在哪里将网格数据绑定?我没有?我使用visual studio将数据网格拖动到其中插入,然后打开一个对话框,在其中我可以将其连接到数据库。您可以在网格视图中查看数据而不绑定网格吗?当我这样做时,我得到1“System.Windows.Forms.DataGridView”不包含“DataBind”的定义,并且没有接受第一个数据绑定的扩展方法“DataBind”可以找到类型为“System.Windows.Forms.DataGridView”的参数(是否缺少using指令或程序集引用?)。这会更好,并自动解决此问题。你应该接受你自己的答案,这将有助于其他人:)嗨,是的,我不得不等两天才能这样做,所以我只是做了谢谢你的反馈
{

                    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();