winforms 插入数据后,Datagridview未刷新

wkftcu5l  于 2023-05-07  发布在  其他
关注(0)|答案(2)|浏览(173)

我知道这个问题已经被问了很多次了,但是相信我,我已经试过了每个问题的所有解决方案,但都不起作用。在执行按钮更新功能并填充数据库之后,数据网格视图应该刷新,但它没有。我尝试了.refesh()和.invalidate()和.update(),但它们也不起作用。
编辑:即使我添加了一个手动刷新按钮,按下它也不会显示最新的数据库信息。只有转到另一个表单,然后返回到此表单,才能显示新数据。
这是我的代码:

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

namespace Construction
{
    public partial class Payments : Form
    {
        //Connection String of the Application
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["myDB"].ToString());
        public Payments()
        {
            InitializeComponent();
            loadData();
        }

        private void loadData()
        {

            //check if connection is closed, if so open it
            if (con.State != ConnectionState.Open)
            {
                con.Open();
            }

            try
            {
                if (String.IsNullOrEmpty(tbPaymentLimit.Text.ToString()))
                    return;
                int limit = Int32.Parse(tbPaymentLimit.Text.ToString());
                string query = "SELECT TOP " + limit + " * FROM MiscPayments WHERE DateTime BETWEEN '" + dtPickerFrom.Value + "' AND '" + dtPickerTo.Value + "';";
                SqlDataAdapter adapter = new SqlDataAdapter(query, con);
                DataTable dt = new DataTable();
                adapter.Fill(dt);
                dgvPayments.DataSource = dt;
                dgvPayments.ResetBindings();
            }
            catch (Exception msg)
            {
                MessageBox.Show("Oops! An error has occured. Please restart the application");
            }

            //close the connection if it is still open
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
        }

        private void btnLogout_Click(object sender, EventArgs e)
        {
            //create the login page
            Login toPage = new Login();
            //show the login page
            toPage.Show();
            //hide the current page
            this.Hide();
        }

        private void btnHomepage_Click(object sender, EventArgs e)
        {
            //create the homepage
            Homepage toPage = new Homepage();
            //show the homepage
            toPage.Show();
            //hide the current page
            this.Hide();
        }

        private void tbPaymentLimit_KeyDown(object sender, KeyEventArgs e)
        {
            loadData();
        }

        private void tbPaymentLimit_Leave(object sender, EventArgs e)
        {
            loadData();
        }

        private void dtPickerFrom_CloseUp(object sender, EventArgs e)
        {
            loadData();
        }

        private void dtPickerTo_CloseUp(object sender, EventArgs e)
        {
            loadData();
        }

        private void btnPaymentUpdate_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(tbPPaid.Text.ToString()) || String.IsNullOrEmpty(tbPDesc.Text.ToString()))
            { return; }

            //check if connection is closed, if so open it
            if (con.State != ConnectionState.Open)
            {
                con.Open();
            }

            try
            {
                String query = "INSERT INTO MiscPayments (Description, Paid, DateTime) VALUES (@Description, @Paid, @DateTime)";
                SqlCommand cmd = new SqlCommand(query, con);
                cmd.Parameters.AddWithValue("@Description", tbPDesc.Text.ToString());
                cmd.Parameters.AddWithValue("@Paid", Double.Parse(tbPPaid.Text.ToString()));
                cmd.Parameters.AddWithValue("@DateTime", DateTime.Now);
                cmd.ExecuteNonQuery();

                loadData();


                MessageBox.Show("Payment of " + tbPPaid.Text.ToString() + " has been successfully added");
                tbPPaid.Text = "";
                tbPDesc.Text = "";
            }

            catch (Exception msg)
            {
                MessageBox.Show("Oops! An error has occured. Please recheck what you entered");
            }

            //close the connection if it is still open
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }

        }
    }
}
jdgnovmf

jdgnovmf1#

问题似乎出在约会时间上。如果我将查询更改为“Select * form MiscPayments”,则表将正常更新。
我通过使用相同的代码创建一个新的load函数来解决这个问题,除了以下内容:string query = "SELECT TOP " + limit + " * FROM MiscPayments WHERE DateTime BETWEEN '" + dtPickerFrom.Value + "' AND '" + DateTime.Now + "';";

jtjikinw

jtjikinw2#

try
            {
                if (String.IsNullOrEmpty(tbPaymentLimit.Text.ToString()))
                    return;
                int limit = Int32.Parse(tbPaymentLimit.Text.ToString());
                string query = "SELECT TOP " + limit + " * FROM MiscPayments WHERE DateTime BETWEEN '" + dtPickerFrom.Value + "' AND '" + dtPickerTo.Value + "';";
                SqlDataAdapter adapter = new SqlDataAdapter(query, con);
                DataTable dt = new DataTable();
                adapter.Fill(dt);
                dgvPayments.DataSource = dt;
                dgvPayments.Refresh(); //Just Refresh the DataGrid it will work 
            }
            catch (Exception msg)
            {
                MessageBox.Show("Oops! An error has occured. Please restart the application");
            }

相关问题