簡體   English   中英

從DataGridView讀取數據僅讀取最后一行C#

[英]Reading data from the DataGridView just reads the last row c#

我已經能夠表明每當工具提示quantity小於5為該productcode與約束,但一旦提示已被證明,它只是閱讀的DataGridView的最后一行,並不是所有的數據更里面。

這是圖片:

在此處輸入圖片說明

從上面的圖像中,您可以看到數量少於5的兩個產品代碼,但是如果從右下角看到,它僅顯示數據的最后一行。

這是我正在使用的代碼:

void CheckQuantity()
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                string productCode = row.Cells[0].Value.ToString();
                decimal quantity = Convert.ToDecimal(row.Cells[1].Value);

                if (quantity < 5)
                {
                    SystemManager.SoundEffect("C:/Windows/Media/Speech Off.wav");

                    customToolTip1.Show("- Product Code: " + productCode + "\n- Quantity: " + quantity, this, _screen.Right, _screen.Bottom, 5000);

                    timeLeft = 15;

                    _timer.Start();
                }

                else
                {
                    timeLeft = 15;

                    _timer.Start();
                }
            }
        }

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

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

                CheckQuantity();
            }
        }

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

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

uint timeLeft = 15;

感謝您的回答。

謝謝。

您所期望的結果還不清楚,這是您的意思嗎?

void CheckQuantity()
        {
            string msg = "";
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                string productCode = row.Cells[0].Value.ToString();
                decimal quantity = Convert.ToDecimal(row.Cells[1].Value);
                if (quantity < 5)
                {
                    msg += "- Product Code: " + productCode + " - Quantity: " + quantity + "\n";
                }
            }
            if (msg != "")
            {
                SystemManager.SoundEffect("C:/Windows/Media/Speech Off.wav");
                customToolTip1.Show(msg, this, _screen.Right, _screen.Bottom, 5000);
            }
        }

void Timer_Tick(object sender, EventArgs e)
        {
            _timer.Stop();
            CheckQuantity();
        }

void Database_Load(object sender, EventArgs e)
        {
            _timer.Interval = 15 * 1000;
            _timer.Start();
        }

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM