I am working on a C# project in which a DataGridView is shown in the WinForm at runtime and needs to be updated every 2~3 seconds. The DataSrouce of each DataGridView is binding to a DataSet which could be changed according to users's operation.
As I am totally a newbie to C#, I have no idea how should I achieve a real-time refresh of the forms in the GUI. Of course I need multi-threading so that the update of the data shown could be done in background and the GUI does not hang.
PS: In Qt, each view is bounded to a model and I can update each model in a different thread from the main GUI thread so that the GUI will not stuck. This is what I am now doing. Sample codes below.
// MainForm.cs
public Bind()
{
dataGridView.DataSource = TableCollection.DataTable1;
}
public StartTimer()
{
System.Windows.Forms.Timer tdfTimer = new System.Windows.Forms.Timer();
guiTimer.Tick += new EventHandler(Refresh);
guiTimer.Interval = 500;
guiTimer.Start();
}
public void Refresh(object sender, EventArgs e)
{
if (backgroundwork == null)
{
backgroundwork = new BackgroundWorker();
backgroundwork.DoWork += delegate(object s, DoWorkEventArgs args) { TableCollection.UpdateData(); };
}
if (!backgroundwork.IsBusy)
backgroundwork.RunWorkerAsync();
}
// TableCollection.cs
class TableCollection
{
static DataTable dataTable1;
public DataTable1
{
get { return dataTable1; }
}
static public void UpdateData()
{
// here i update each row in dataTable1
}
}
Aucun commentaire:
Enregistrer un commentaire