progress.minmum = 0;
progress.step = 1;
progress.maxmum = dataSet.Tables[0].Rows.Count;
foreach( DataRow dr in dataSet.Tables[0].Rows )
{
  progress.PerformStep();
}

解决方案 »

  1.   

    to:maotin(liu)
    但是这样不能同步啊,我要的是fill的时候就显示,你这样是fill后才行!
      

  2.   

    显然,提问者的意思是说从库中获取数据时候显示进度条,而不是已经填充到
    dataset后操作数据时候显示进度条。但是如果使用
    SqlDataAdapter sda = ...
    sda.Fill(ds);
    的话,就会一次将数据读入DataSet。即使使用多线程的话,也不能让进度条中
    的进度合理显示。所以应该改用:
    //先获取全部记录条数,这个即使是大表,速度也应该比较快
    SqlCommand sc = new SqlCommand("select count(*) from table1",sqlconn);
    int count = (int) sc.ExecuteScalar();
    progress1.Step = 1;  //设置步进为1
    int pernum = count/100;  //设置每次分步Fill的记录条数
    int start = 0; 
    DataSet ds = new DataSet();
    SqlDataAdapter sda = new SqlDataAdapter(.....);
    //以下在另一线程里从数据库读数据到DataSet中
    int end = (start+pernum>=count)?count-1 : start+pernum;
    sda.Fill(ds,start,end,"mytable");
     //操作主线程中的progress1增加一步
    if(end>=count)
        progress1.SetPos(100);
    else
        progress1.StepIt();通过分步Fill数据集和多线程的配合就可以做到。不过,如果采用
    DataReader和多线程配合更简单些(因为可以在DataReader的read
    方法中设置进度),只是使用Fill DataSet便于后期数据操作。