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

时间:2014-11-11 12:51:20

标签: c# winforms datagridview

我有2个表单,数据库和付款数据库表单有DataGridView,内部DataGridViewQuantity ,当Quantity小于5时,我已经可以显示工具提示,并且每15秒显示一次。现在,我希望在付款表单中显示与数据库表单相同的内容,当然还有{{>修饰符在{{ 数据库表单中的1}}必须为DataGridView才能访问它。我已经这样做并在付款表单中初始化数据库表单。运行付款表单后,它应该每隔15秒显示与数据库表单类似的工具提示,而public上的Quantity则显示在数据库表单中小于5,但它没有显示。我无法弄明白为什么以及如何解决这个问题。

任何人都可以为我解决这个问题吗?

以下是数据库表单中工具提示的图片(注意DataGridView小于5,所有这些都是:

enter image description here

以下是我正在使用的代码:

数据库表单:

Quantity

以下是付款表单的代码:

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

1 个答案:

答案 0 :(得分:0)

解决了,我从数据库(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();
                }
            }
        }

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