C# 将数据插入表后,如何刷新GridView控件?

C# 将数据插入表后,如何刷新GridView控件?,c#,winforms,gridview,C#,Winforms,Gridview,我正在尝试在插入数据后刷新GridView。我尝试了很多选择,但没有一个对我有效。我如何解决这个问题 这是我的密码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using S

我正在尝试在插入数据后刷新
GridView
。我尝试了很多选择,但没有一个对我有效。我如何解决这个问题

这是我的密码:

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

namespace DigitalJournalDNAIsolation
{
    public partial class Form1 : Form
    {
        private List<Scan> scanList;

        private SqlConnection connection;

        public Form1()
        {
            InitializeComponent();

            scanList = new List<Scan>();
        }

        private void openDatabase()
        {
            connection = new SqlConnection();
            connection.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\prp\Documents\Visual Studio 2010\Projects\DigitalJournalDNAIsolation\DigitalJournalDNAIsolation\Database1.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
            connection.Open();
        }

        private void closeDatabase()
        {
            connection.Close();
        }


        //Add items to the list
        private void textBoxBarCode_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                SaveToDataBase();



                textBoxBarCode.Clear();
            }

        }

        //What machine has bees selected by the user
        public string machineSelected(string machine)
        {
            bool mmpp1 = checkBoxMMPP1.Checked;
            bool mmpp2 = checkBoxMMPP2.Checked;


            if (mmpp1 == true && mmpp2 == false)
            {
                return machine = "MMPP-1";
            }

            if (mmpp1 == false && mmpp2 == true)
            {
                return machine = "MMPP-2";
            }

            if (mmpp1 == true && mmpp2 == true)
            {
                //MessageBox.Show("Select either MMPP-1 or MMPP-2", "Please select a machine");
                return machine = "Multiple machines are selected";
            }
            else
            {
                //MessageBox.Show("Select either MMPP-1 or MMPP-2", "Please select a machine");
                return machine = "No machine selected";
            }


        }
        public string machine { get; set; }

        public string bufferStraat(string straat)
        {
            bool manual = checkBoxManual.Checked;
            bool janus = checkBoxJanus.Checked;


            if (manual == true && janus == false)
            {
                return straat = "Manual";
            }

            if (manual == false && janus == true)
            {
                return straat = "Janus";
            }

            if (manual == true && janus == true)
            {
                //MessageBox.Show("Select either Manual or Janus", "Please select a machine");
                return straat = "Multiple machines are selected";
            }
            else
            {
                //MessageBox.Show("Select either Manual or Janus", "Please select a machine");
                return straat = "No machine is selected";
            }
        }

        public string straat { get; set; }

        public void SaveToDataBase()
        {
            string date = DateTime.Now.ToLongDateString();

            openDatabase();

            SqlCommand command = new SqlCommand();
            command.Connection = connection;
            command.CommandType = CommandType.Text;
            command.CommandText = "INSERT INTO Table1 (box,username,comment,machine,buffers,date) VALUES (@box,@username,@comment,@machine,@buffers,@date)";

            command.Parameters.Add("@box", SqlDbType.VarChar).Value = textBoxBarCode.Text;
            command.Parameters.Add("@username", SqlDbType.VarChar).Value = textBoxUser.Text;
            command.Parameters.Add("@comment", SqlDbType.VarChar).Value = textBoxComment.Text;
            command.Parameters.Add("@machine", SqlDbType.VarChar).Value = machineSelected(machine);
            command.Parameters.Add("@buffers", SqlDbType.VarChar).Value = bufferStraat(straat);
            command.Parameters.Add("@date", SqlDbType.VarChar).Value = date;

            command.ExecuteNonQuery();

            closeDatabase();
        }

        public void refreshGrid1()
        {
            openDatabase();

            SqlCommand readcmd = new SqlCommand();
            readcmd.Connection = connection;
            readcmd.CommandType = CommandType.Text;
            readcmd.CommandText = "SELECT * FROM Table1";

            SqlDataReader reader = null;

            reader = readcmd.ExecuteReader();

            closeDatabase();
        }


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


        private void infoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (AboutBox1 about = new AboutBox1())
            {
                about.ShowDialog();
            }
        }

        private void bestandToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用扫描动作;
使用System.Data.SqlClient;
命名空间数字日志隔离
{
公共部分类Form1:Form
{
私人名单扫描名单;
专用SqlConnection;
公共表格1()
{
初始化组件();
scanList=新列表();
}
私有void openDatabase()
{
connection=newsqlconnection();
connection.ConnectionString=@“数据源=。\SQLEXPRESS;AttachDbFilename=C:\Users\prp\Documents\Visual Studio 2010\Projects\DigitalJournalNaisolation\DigitalJournalNaisolation\Database1.mdf;集成安全性=True;连接超时=30;用户实例=True”;
connection.Open();
}
私有数据库()
{
connection.Close();
}
//将项目添加到列表中
private void textBoxBarCode_按键(对象发送器,按键事件参数e)
{
如果(e.KeyChar==(char)13)
{
SaveToDataBase();
textBoxBarCode.Clear();
}
}
//用户选择了什么机器
已选择公共字符串计算机(字符串计算机)
{
bool mmpp1=checkBoxMMPP1.Checked;
bool mmpp2=checkBoxMMPP2.Checked;
如果(mmpp1==true&&mmpp2==false)
{
返回机器=“MMPP-1”;
}
如果(mmpp1==false&&mmpp2==true)
{
返回机器=“MMPP-2”;
}
如果(mmpp1==true&&mmpp2==true)
{
//MessageBox.Show(“选择MMPP-1或MMPP-2”,“请选择一台机器”);
return machine=“选择多台机器”;
}
其他的
{
//MessageBox.Show(“选择MMPP-1或MMPP-2”,“请选择一台机器”);
return machine=“未选择机器”;
}
}
公共字符串计算机{get;set;}
公共字符串缓冲区(字符串缓冲区)
{
bool manual=复选框manual.选中;
bool janus=checkBoxJanus.Checked;
如果(手动==true&&janus==false)
{
return straat=“手动”;
}
如果(手动==false&&janus==true)
{
return straat=“Janus”;
}
如果(手动==true&&janus==true)
{
//MessageBox.Show(“选择手动或Janus”,“请选择一台机器”);
return straat=“选择了多台机器”;
}
其他的
{
//MessageBox.Show(“选择手动或Janus”,“请选择一台机器”);
return straat=“未选择任何机器”;
}
}
公共字符串{get;set;}
public void SaveToDataBase()
{
string date=DateTime.Now.ToLongDateString();
openDatabase();
SqlCommand=newsqlcommand();
command.Connection=连接;
command.CommandType=CommandType.Text;
command.CommandText=“将值(@box、@username、@comment、@machine、@buffers、@date)插入表1(框、用户名、注释、机器、缓冲区、日期));
command.Parameters.Add(“@box”,SqlDbType.VarChar).Value=textBoxBarCode.Text;
command.Parameters.Add(“@username”,SqlDbType.VarChar).Value=textBoxUser.Text;
Add(“@comment”,SqlDbType.VarChar).Value=textBoxComment.Text;
command.Parameters.Add(“@machine”,SqlDbType.VarChar).Value=machineSelected(machine);
command.Parameters.Add(“@buffers”,SqlDbType.VarChar).Value=bufferStraat(straat);
command.Parameters.Add(“@date”,SqlDbType.VarChar).Value=date;
command.ExecuteNonQuery();
closeDatabase();
}
公共文件1()
{
openDatabase();
SqlCommand readcmd=new SqlCommand();
readcmd.Connection=连接;
readcmd.CommandType=CommandType.Text;
readcmd.CommandText=“从表1中选择*”;
SqlDataReader=null;
reader=readcmd.ExecuteReader();
closeDatabase();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
//TODO:这行代码将数据加载到“database1DataSet.Table1”表中。您可以根据需要移动或删除它。
this.table1TableAdapter.Fill(this.database1DataSet.Table1);
}
私有void infoToolStripMenuItem\u单击(对象发送方,事件参数e)
{
使用(AboutBox1 about=new AboutBox1())
{
about.ShowDialog();
}
}
私有void bestandToolStripMenuItem\u单击(对象发送者,事件参数e)
{
这个。关闭();
}
}
}

使用gridView的数据绑定方法

 public void refreshGrid1()
    {
        openDatabase();

        SqlCommand readcmd = new SqlCommand();
        readcmd.Connection = connection;
        readcmd.CommandType = CommandType.Text;
        readcmd.CommandText = "SELECT * FROM Table1";

        SqlDataReader reader = null;

        reader = readcmd.ExecuteReader();

        closeDatabase();

        gridView.DataBind();

    }

使用gridView的数据绑定方法

 public void refreshGrid1()
    {
        openDatabase();

        SqlCommand readcmd = new SqlCommand();
        readcmd.Connection = connection;
        readcmd.CommandType = CommandType.Text;
        readcmd.CommandText = "SELECT * FROM Table1";

        SqlDataReader reader = null;

        reader = readcmd.ExecuteReader();

        closeDatabase();

        gridView.DataBind();

    }
尝试这样做:-

1) 。创建绑定源并将此对象的数据源设置为 数据表

2) 。将DataGrid的数据源设置为 绑定源

现在,当您提交对DataTable的更改时,它们将自动反映在您的GridView中。

尝试执行以下操作:-

1) 。创建绑定源并将此对象的数据源设置为 数据表

2) 。设定