简体   繁体   中英

Retrieve the data from another form datagridview c#

I have 2 forms, Database and Payment , and the Database form have a DataGridView and inside DataGridView have Quantity , I already could show the tooltip whenever the Quantity less than 5 and it is shows every 15 seconds. Now, I wanted to show the same thing that I did on the Database form in the Payment form and of course the modifier on the DataGridView in the Database form have to be public in order to access it. I already done that and initialize the Database form in the Payment form. Once I run the Payment form, it should show the tooltip every 15 seconds that similar with the Database form, while the Quantity on the DataGridView in the Database form less than 5, but it is not showing. I could not figure it out why and how to fix that.

Anyone could solve this for me?

Here is the image of the tooltip in the Database form (notice that the Quantity less than 5, all of them):

在此处输入图片说明

Here is the code that I am using:

Database form:

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

And here is the code for Payment form:

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

Solved, I am retrieve the data from the Database (Access Database) instead of from the Data Grid View:

I will post the code if people wondering how I done it:

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

Anyway, thank you guys who read this question in the first place.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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