简体   繁体   English

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

[英]How do I refresh a GridView control after inserting data into the table?

I am trying to refresh a GridView after inserting the data. 我正在尝试在插入数据后刷新GridView I have tried a lot of options but none of the worked for me. 我尝试了很多选择,但没有一个适合我。 How can I resolve this problem? 我该如何解决这个问题?

Here is my code: 这是我的代码:

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

Use gridView's DataBind method 使用gridView的DataBind方法

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

    }

Try doing this :- 尝试这样做:

1). 1)。 Create a Binding Source and set the Datasource for this object to the DataTable. 创建一个绑定源并将该对象的数据源设置为DataTable。

2). 2)。 Set the Datasource for your DataGrid to that of the Binding Source. 将您的DataGrid的数据源设置为绑定源的数据源。

Now, When ever you commit changes to the DataTable, they will automatically reflect in your GridView. 现在,无论何时将更改提交到DataTable,它们都将自动反映在GridView中。

This is how i solved the problem: 这就是我解决问题的方式:

    private void openAllFromDatabase()
    {

        openConnection();

        string query = "select * from my_Table order by date desc";

        DataSet dset = new DataSet();
        SqlDataAdapter adapter = new SqlDataAdapter(query, connection);

        SqlCommandBuilder cb = new SqlCommandBuilder(adapter);

        adapter.Fill(dset, "my_Table");

        dataGridView1.DataSource = dset;
        dataGridView1.DataMember = "my_table";

        adapter.Update(dset, "my_Table");

        closeConnection();


    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM