简体   繁体   中英

C# DataGridView Refresh on Specified Interval

I have a best practice question. I'm building a Windows forms application in C#/.NET 4.0 where "scans" will take place behind the scenes every X seconds and update a database. I'd like to a DataGridView refresh on a specified interval to reflect the data that is now represented in the database. I plan on doing this by adding a timer to the form and on the tick event, just re-populate the DataGridView. I was wondering if this approach is the best method or if there is a more recommended way to do this?

For datagridview refresh on specified interval use multithreading with delegate.Its better for memory management.

check this

If you use an MVVM pattern an bind your datagrid data source to a collection wich raise property changed every time it's changed (Mode = TwoWay) Your data grid will be updated automatically...

    private ObservableCollection<Data>_dataCollection ;
    public ObservableCollection<Data> DataCollection 
    {
        get { return _dataCollection ; }
        set
        {
            _dataCollection = value;
            RaisePropertyChanged("DataCollection ");
        }
    }

And the XAML code is :

     <DataGrid DataSource="{Binding Mode=TwoWay, Path=DataCollection}"
               DataContext="YourViewModel">

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