繁体   English   中英

从另一个表单datagridview c#中检索数据

[英]Retrieve the data from another form datagridview c#

我有2个表单, Database和Payment ,并且Database表单有一个DataGridView并且在DataGridViewQuantity ,只要Quantity小于5,我已经可以显示工具提示,并且每15秒显示一次。 现在,我想显示与在“ 付款”表单中的“ 数据库”表单上所做的相同的事情,当然,为了访问它,必须对“ 数据库”表单中的DataGridView上的修饰符进行public 我已经做到了,并在“ 付款”表单中初始化“ 数据库 ”表单。 一旦运行“ 付款”表单,它应该每15秒显示一次与数据库表单类似的工具提示,而“ 数据库”表单中DataGridView上的“ Quantity ”小于5,但不会显示。 我不知道为什么以及如何解决该问题。

有人可以为我解决这个问题吗?

这是“ 数据库”窗体中的工具提示的图像(请注意, Quantity小于5,所有这些都小于):

在此处输入图片说明

这是我正在使用的代码:

数据库形式:

public partial class Database : Form
    {
        uint timeLeft = 15;

        Timer _timer = new Timer();

        Rectangle _screen;

        public Database()
        {
            InitializeComponent();

            _timer.Interval = 1000;

            _timer.Tick += Timer_Tick;
        }

        void Database_Load(object sender, EventArgs e)
        {
            _timer.Start();
        }

        void Database_FormClosed(object sender, FormClosedEventArgs e)
        {
            _timer.Stop();
        }

        void Timer_Tick(object sender, EventArgs e)
        {
            timeLeft--;

            if (timeLeft == 0)
            {
                _timer.Stop();

                CheckQuantity();
            }
        }

        void CheckQuantity()
        {
            string message = string.Empty;

            if (customDataGridView1.Rows.Count != 0)
            {
                foreach (DataGridViewRow row in customDataGridView1.Rows)
                {
                    string productCode = row.Cells[0].Value.ToString();
                    decimal quantity = Convert.ToDecimal(row.Cells[1].Value);

                    if (quantity < 5)
                    {
                        message += "- Product Code: " + productCode + "\n- Quantity: " + quantity + "\n\n";

                        timeLeft = 15;

                        _timer.Start();
                    }

                    else if (quantity >= 5)
                    {
                        timeLeft = 15;

                        _timer.Start();
                    }

                }

                if (message != string.Empty)
                {
                    SystemManager.SoundEffect("C:/Windows/Media/Speech Off.wav");

                    customToolTip1.Show("The system has detected the following: \n\n" + message + "Have quantity less than 5.\nPlease update them immediately.", this, _screen.Right, _screen.Bottom, 5000);
                }
            }

            else
            {
                _timer.Start();
            }
        }

这是付款表格的代码:

public partial class Payment : Form
    {
        uint timeLeft = 15;

        Database _database = new Database();

        Timer _timer = new Timer();

        Rectangle _screen;

        public Payment()
        {
            InitializeComponent();

            _timer.Interval = 1000;

            _timer.Tick += Timer_Tick;
        }

        void Payment_Load(object sender, EventArgs e)
        {
            _timer.Start();
        }

        void Payment_FormClosed(object sender, FormClosedEventArgs e)
        {
            _timer.Stop();
        }

        void Timer_Tick(object sender, EventArgs e)
        {
            timeLeft--;

            if (timeLeft == 0)
            {
                _timer.Stop();

                CheckQuantity();
            }
        }

        void CheckQuantity()
        {
            string message = string.Empty;

            if (_database.customDataGridView1.Rows.Count != 0)
            {
                foreach (DataGridViewRow row in _database.customDataGridView1.Rows)
                {
                    string productCode = row.Cells[0].Value.ToString();
                    decimal quantity = Convert.ToDecimal(row.Cells[1].Value);

                    if (quantity < 5)
                    {
                        message += "- Product Code: " + productCode + "\n- Quantity: " + quantity + "\n\n";

                        timeLeft = 15;

                        _timer.Start();
                    }

                    else if (quantity >= 5)
                    {
                        timeLeft = 15;

                        _timer.Start();
                    }

                }

                if (message != string.Empty)
                {
                    SystemManager.SoundEffect("C:/Windows/Media/Speech Off.wav");

                    customToolTip1.Show("The system has detected the following: \n\n" + message + "Have quantity less than 5.\nPlease update them immediately.", this, _screen.Right, _screen.Bottom, 5000);
                }
            }

            else
            {
                _timer.Start();
            }
        }

解决了,我从数据库(Access数据库)而不是从数据网格视图中检索数据:

如果有人想知道我是如何做到的,我将发布代码:

public static void GetQuantity()
        {
            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {
                string query = "SELECT [Quantity] FROM [Database]";

                connection.Open();

                using (OleDbCommand command = new OleDbCommand(query, connection))
                {
                    using (OleDbDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            int quantity = (int)reader["Quantity"];

                            UserInformation.Quantity = Convert.ToDecimal(quantity);
                        }

                        reader.Close();
                    }
                }

                connection.Close();
            }
        }

        public static void CheckQuantity(CustomToolTip _customToolTip, IWin32Window _window, int _x, int _y, int _duration)
        {
            GetQuantity();

            string message = string.Empty;

            string productCode = string.Empty;

            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {
                string query = "SELECT [ProductCode] FROM [Database] WHERE [Quantity] = @Quantity";

                connection.Open();

                using (OleDbCommand command = new OleDbCommand(query, connection))
                {
                    command.Parameters.Add("@Quantity", OleDbType.Decimal);
                    command.Parameters["@Quantity"].Value = UserInformation.Quantity;

                    using (OleDbDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            productCode = (string)reader["ProductCode"];

                            if (UserInformation.Quantity < 5)
                            {
                                message += "- Product Code: " + productCode + "\n- Quantity: " + UserInformation.Quantity + "\n\n";
                            }
                        }

                        if (message != string.Empty)
                        {
                            SystemManager.SoundEffect("C:/Windows/Media/Speech Off.wav");

                            _customToolTip.Show("The system has detected the following: \n\n" + message + "Have quantity less than 5.\nPlease update them immediately.", _window, _x, _y, _duration);
                        }

                        reader.Close();
                    }

                }

                connection.Close();
            }
        }

 void Timer_Tick(object sender, EventArgs e)
        {
            this.textBox4.Text = DateTime.Now.ToString("dd - MMM - yyyy hh:mm:ss tt");

            timeLeft--;

            if (timeLeft == 0)
            {
                _timer.Stop();

                if (UserInformation.Quantity < 5)
                {
                    SystemManager.CheckQuantity(customToolTip1, this, _screen.Right, _screen.Bottom, 5000);

                    timeLeft = 15;

                    _timer.Start();
                }

                else if (UserInformation.Quantity >= 5)
                {
                    timeLeft = 15;

                    _timer.Start();
                }
            }
        }

无论如何,谢谢那些首先阅读此问题的人。

暂无
暂无

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

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