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

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

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

string pervCount = "0";
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:

conn.Open();
SqlCommand cmd = new SqlCommand("SELECT COUNT(InvoiceID) FROM InvoicesHeaderTable;", conn);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
    newCount = dr.GetValue(0).ToString();
}
conn.Close();

Then I'm checking:

if (pervCount != newCount)
{
    conn.Open();
    SqlCommand cmd1 = new SqlCommand("select InvoiceID, CustomerName, TotalInvoicePrice from InvoicesHeaderTable where CompleteStatus=0", conn);
    SqlDataAdapter da = new SqlDataAdapter(cmd1);
    DataTable dt = new DataTable();
    da.Fill(dt);
    if (dt.Rows.Count > 0)
    {
        CashierDgv.DataSource = dt;
    }
    conn.Close();
    pervCount = newCount;
}

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

string pervCount = "0";
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:

            conn.Open();
            SqlCommand cmd = new SqlCommand("SELECT COUNT(InvoiceID) FROM InvoicesHeaderTable;", conn);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                newCount = dr.GetValue(0).ToString();
            }
            conn.Close();

then i'm checking:

            if (pervCount != newCount)
            {
                conn.Open();
                SqlCommand cmd1 = new SqlCommand("select InvoiceID, CustomerName,TotalInvoicePrice from InvoicesHeaderTable where CompleteStatus=0", conn);
                SqlDataAdapter da = new SqlDataAdapter(cmd1);
                DataTable dt = new DataTable();
                da.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    CashierDgv.DataSource = dt;
                }
                conn.Close();
                pervCount = newCount;
            }

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

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

int pervCount = 0;

在我的定时器外部,然后在我的定时器内部:

SqlCommand cmd = new SqlCommand("SELECT InvoiceID,CustomerName,TotalInvoicePrice FROM InvoicesHeaderTable where CompleteStatus =0", conn);
SqlDataAdapter adab = new SqlDataAdapter(cmd);
DataTable table = new DataTable();
adab.Fill(table);
if (table.Rows.Count > pervCount)
{
    CashierDgv.DataSource = table;
    pervCount = table.Rows.Count;
}
英文:

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

int pervCount = 0;

out of my timer, then inside my timer:

            SqlCommand cmd = new SqlCommand("SELECT InvoiceID,CustomerName,TotalInvoicePrice FROM InvoicesHeaderTable where CompleteStatus =0", conn);
            SqlDataAdapter adab = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adab.Fill(table);
            if (table.Rows.Count > pervCount)
            {
                CashierDgv.DataSource = table;
                pervCount = table.Rows.Count;
            }

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:

确定