Update Counter not updating my DataGridView

时间:2016-02-12 19:24:17

标签: c# datagridview visual-studio-2015 counter

I'm creating a Windows Form App in Visual Studio 2015. I have it communicating and sharing with our SQL Server perfectly for every function but one. I'd like to place a "live" counter on the form, that updates a value every 3(or something) seconds. The counter's job would be to keep track of inventory being shipped out of our warehouse (just need to worry about getting that info from our SQL Server, which already has the capabilities, nothing before that) and display that information. Doesn't have to be fancy, just accurate.

public void InitializeTimer()
    {
        var timer = new System.Timers.Timer();
        timer.Interval = 3;
        timer.Start();
        timer.Elapsed += keep_track_of_inventory_being_shipped;
    }

    private void keep_track_of_inventory_being_shipped(object sender, EventArgs e)
    {
        RefreshMyForm();
    }

    private void RefreshMyForm()
    {
        this.TableAdapter.Fill(this.DataSet.CMS);
    }
}

I had asked this question before (dynamic item counter from SQLServer 2014 in web form for Visual Studio 2015?) and I had thought I got it right but it was brought to my attention I was mistaken. So sadly I'm back... I have tried to modify the code with research on SO and other sources to get it working so this code looks different than the code in the URL I posted in the past. No dice.

Additional info: When the program first loads, "this.TableAdapter.Fill(this.DataSet.CMS);" loads the counter I want refreshed every few seconds (or minutes, whatever. I'll worry about that after the counter is working). It certainly does bring up the right count on program start, but I can't seem to get it to refresh at all. I am aware the last two methods may be overkill, but I decided to just leave it as is and ask. I started with the low interval just to check and see if it's updating. It's not :/ I don't get why there are no errors thrown up or why "this.TableAdapter.Fill(this.DataSet.CMS);" can update the count correctly from our database at start, but cannot be refreshed this way? If it's not asking too much, I'd prefer understanding this as well as suggestions to fix instead of just a fix.

The original solution posted by tchelidze was the one I previously went with, but I'm sure blame lies with me not implementing his suggestion correctly. I appreciate any help I can get, long time SO lurker here and I love the site/meta. Thanks all.

1 个答案:

答案 0 :(得分:1)

这是因为你需要在进入timer.Stop()时拨打RefreshMyForm,然后在填写方法完成后再拨打timer.Start(),时间无法访问这个方法public void InitializeTimer()你应该在表单上放一个计时器并按照你应该从RefreshMyForm

调用timer1_Tick Event的方式执行
相关问题