The progress you need to get is from business logic code but not DataGridView. For example, when you read data from database, you can create a model dialog window and use multi-threading technology to show the progress on the model dialog window, beause a model window can block the current thread and cause the application not responding.
http://hi.baidu.com/xdragon/blog/item/b845c650b745f0591138c253.html
{
DataCollection<AnswerPaperSDO> source = this.Input;
if (source == null||source.Count==0)
{
MessageBox.Show("请先查找试卷");
return;
}
toolStripProgressBar1.Minimum = 0;
toolStripProgressBar1.Maximum = source.Count;
status_Progress.Text = "0/" + source.Count.ToString();
backgroundWorker1.RunWorkerAsync(source);
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
toolStripProgressBar1.Value = e.ProgressPercentage;
status_Progress.Text = toolStripProgressBar1.Value.ToString() + "/" + toolStripProgressBar1.Maximum.ToString();
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (!e.Cancelled)
MessageBox.Show("学生答卷下载完毕!");
else
MessageBox.Show("取消下载");
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
Serailize sera = new Serailize(); if (e.Cancel)
{
MessageBox.Show("ff");
return;
}
DataCollection<AnswerPaperSDO> answerPaperCollection = e.Argument as DataCollection<AnswerPaperSDO>;
for (int i = 0; i < answerPaperCollection.Count; i++)
{
if (backgroundWorker1.CancellationPending)
{
e.Cancel = true;
return;
}
AnswerPaperSDO data = answerPaperCollection[i];
//找到目录下的试卷目录,
//如果找到答卷,继续
//没有写文件
//answerPaper.PaperId
string paperPath = getWorkFolder() + data.PaperId.ToString();
if (!Directory.Exists(paperPath))
Directory.CreateDirectory(paperPath);
string answerPath = paperPath + "\\" +data.StudentCode+"——"+ data.AnswerPaperId.ToString();
if (!Directory.Exists(answerPath))
Directory.CreateDirectory(answerPath);
string paperName = paperPath + "\\" + data.PaperId.ToString();
if (!File.Exists(paperName))
{
PaperFullSDO paper = getPaperService().FindFullByID(data.PaperId);
sera.ObjectToBin(paper, paperName, false);
} string filePath = answerPath + "\\" + data.AnswerPaperId.ToString();
if (!File.Exists(filePath))
{
AnswerPaperFullSDO answerPaper = getProxy().FindSimpleByID(data.AnswerPaperId);
sera.ObjectToBin(answerPaper, filePath, false);
}
backgroundWorker1.ReportProgress(i + 1);
}
}