I have a windows form app written in C#. I took all the code that, (does a restapi call,builds a list, puts the list into a datagridview) into a thread so I can check the thread and update the customer as it is running. When doing this everything works except the scroll bars are greyed out.
Is there a problem with scrollbars only?
If your code still in main thread, your form isn't able to do anything.(you need double check)
But it is true, you can use Application.DoEvents();
after finish bind data to gridView. This method will stop all thread and update view, please make sure this is not a perfect solution, this method will bring irreversible error sometimes when cross thread condition.
Anyway, I hope to use System.Windows.Forms.Timer
to make it simple.
Here is sample.
After starting form, timer will work 100 times and interval is 5 seconds.
private int loopCnt = 1;
// you don't need to this, just drag timer component from a "tool box"
// to your form any where at design time(mode).
// private System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
private void Form1_Load(object sender, EventArgs e)
{
//it is up to you start timer at form-load or triggering after some button click.
//this.loopCnt = 1;
this.timer1.Enabled = true;
this.timer1.Interval = 1000 * 5;
this.timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
CreateDataAndBind(loopCnt++);
if(loopCnt > 100)
{
this.timer1.Stop();
}
}
public void CreateDataAndBind(int loopCount)
{
DataSet dsTemp = new DataSet();
#region : Define data Table and columns
DataTable dtTemp = new DataTable();
DataColumn dColKey = new DataColumn("key", typeof(int));
DataColumn dColName = new DataColumn("name", typeof(String));
dtTemp.Columns.Add(dColKey);
dtTemp.Columns.Add(dColName);
dtTemp.Columns.Add(new DataColumn("role"));
dtTemp.Columns.Add(new DataColumn("timeText", typeof(String)));
dtTemp.Columns.Add(new DataColumn("timeValue", typeof(DateTime)));
#endregion
dsTemp.Tables.Add(dtTemp);
for (int index = 0; index < 10; index++)
{
DataRow dRow = dtTemp.NewRow();
dRow[0] = loopCount;
dRow[1] = "jornathan";
dRow[2] = "Developer";
dRow[3] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
dRow[4] = DateTime.Now;
dtTemp.Rows.Add(dRow);
}
this.BindDataSet(dsTemp);
}
public delegate void updateDataSetDelegate(DataSet ds);
private void BindDataSet(DataSet ds)
{
if (this.dataGridView1.InvokeRequired)
{
this.Invoke(new updateDataSetDelegate(BindDataSet), new object[] { ds });
}
else
{
this.dataGridView1.DataSource = ds.Tables[0];
}
}
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.