我正在使用WinForm C#计时器遇到奇怪的行为。

huangapple go评论103阅读模式
英文:

i'm experiencing a weird behavior with winform c# timer

问题

I'm trying to update datagridview from sql server database using a timer. First, I got 2 strings

  1. string pervCount = "0";
  2. string newCount = "0";

They are out of my timer; the rest is inside the timer (edit1). Then I'm counting the rows in my SQL table using:

  1. conn.Open();
  2. SqlCommand cmd = new SqlCommand("SELECT COUNT(InvoiceID) FROM InvoicesHeaderTable;", conn);
  3. SqlDataReader dr = cmd.ExecuteReader();
  4. while (dr.Read())
  5. {
  6. newCount = dr.GetValue(0).ToString();
  7. }
  8. conn.Close();

Then I'm checking:

  1. if (pervCount != newCount)
  2. {
  3. conn.Open();
  4. SqlCommand cmd1 = new SqlCommand("select InvoiceID, CustomerName, TotalInvoicePrice from InvoicesHeaderTable where CompleteStatus=0", conn);
  5. SqlDataAdapter da = new SqlDataAdapter(cmd1);
  6. DataTable dt = new DataTable();
  7. da.Fill(dt);
  8. if (dt.Rows.Count > 0)
  9. {
  10. CashierDgv.DataSource = dt;
  11. }
  12. conn.Close();
  13. pervCount = newCount;
  14. }

The if statement should not work since pervCount = newCount since I'm assigning both variables to be equal in the if statement. But somehow my datagridview is constantly refreshed.

英文:

i'm trying to update datagridview from sql server database using a timer. First i got 2 strings

  1. string pervCount = "0";
  2. string newCount = "0";

they are out of my timer, the rest is inside the timer (edit1)
then i'm counting the rows in my sql table using:

  1. conn.Open();
  2. SqlCommand cmd = new SqlCommand("SELECT COUNT(InvoiceID) FROM InvoicesHeaderTable;", conn);
  3. SqlDataReader dr = cmd.ExecuteReader();
  4. while (dr.Read())
  5. {
  6. newCount = dr.GetValue(0).ToString();
  7. }
  8. conn.Close();

then i'm checking:

  1. if (pervCount != newCount)
  2. {
  3. conn.Open();
  4. SqlCommand cmd1 = new SqlCommand("select InvoiceID, CustomerName,TotalInvoicePrice from InvoicesHeaderTable where CompleteStatus=0", conn);
  5. SqlDataAdapter da = new SqlDataAdapter(cmd1);
  6. DataTable dt = new DataTable();
  7. da.Fill(dt);
  8. if (dt.Rows.Count > 0)
  9. {
  10. CashierDgv.DataSource = dt;
  11. }
  12. conn.Close();
  13. pervCount = newCount;
  14. }

the if statement should not work since pervCount = newCount since i'm a assigning both variables to be equal int the if statement. But some how my datagridview is constantly refreshed.

答案1

得分: 0

我使用了一个简化的代码,解决了我的问题,实现了我想要的效果。

  1. int pervCount = 0;
  2. 在我的定时器外部,然后在我的定时器内部:
  3. SqlCommand cmd = new SqlCommand("SELECT InvoiceID,CustomerName,TotalInvoicePrice FROM InvoicesHeaderTable where CompleteStatus =0", conn);
  4. SqlDataAdapter adab = new SqlDataAdapter(cmd);
  5. DataTable table = new DataTable();
  6. adab.Fill(table);
  7. if (table.Rows.Count > pervCount)
  8. {
  9. CashierDgv.DataSource = table;
  10. pervCount = table.Rows.Count;
  11. }
英文:

i used a simpler code that fixed my problem and did exactly what i want.

  1. int pervCount = 0;

out of my timer, then inside my timer:

  1. SqlCommand cmd = new SqlCommand("SELECT InvoiceID,CustomerName,TotalInvoicePrice FROM InvoicesHeaderTable where CompleteStatus =0", conn);
  2. SqlDataAdapter adab = new SqlDataAdapter(cmd);
  3. DataTable table = new DataTable();
  4. adab.Fill(table);
  5. if (table.Rows.Count > pervCount)
  6. {
  7. CashierDgv.DataSource = table;
  8. pervCount = table.Rows.Count;
  9. }

huangapple
  • 本文由 发表于 2023年5月15日 03:10:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249265.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定