C#使用winform实现进度条效果

  private void button7_Click(object sender, EventArgs e)

  {

  if (dataGridView4.DataSource == null)

  {

  MessageBox.Show("没有需要上传的数据", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

  }

  else

  {

  int sumcout = 0;

  int upcount = 0;

  foreach (DataGridViewRow row in dataGridView4.SelectedRows)

  {

  sumcout += 1;

  progressBar1.Value++;//动态显示进度条进度

  Application.DoEvents();//处理当前在消息队列中的所有windows消息

  string orgid = row.Cells["StaID"].Value.ToString();

  string empid = row.Cells["EmpID"].Value.ToString();

  Det_SummaryDataContext det = new Det_SummaryDataContext();

  string cityid = (from sys in det.Base_SystemParameter

  select sys.CityID).ToList()[0];

  root r = new root();

  head h = new head();

  body b = new body();

  string strxml = string.Empty;

  string restr = string.Empty;

  ServiceReference1.VeptsServiceImplClient vsc = new ServiceReference1.VeptsServiceImplClient();

  int evl = 0;

  var query1 = from ur in det.Peo_TUserRole

  join ri in det.Peo_RoleInfo on ur.RoleID equals ri.RoleID into A

  from urri in A.DefaultIfEmpty()

  select new

  {

  UADID = ur.UADID,

  RoleID = ur.RoleID,

  RoleName = urri.RoleName

  };

  var query2 = from q in query1

  join tu in det.Peo_TUserRightRelation on q.UADID equals tu.UADID into B

  from qtu in B.DefaultIfEmpty()

  select new

  {

  UADID = q.UADID,

  RoleID = q.RoleID,

  RoleName = q.RoleName,

  EmpID = qtu.EmpID

  };

  var query = from q in query2

  join ei in det.Peo_EmployeeInfo on q.EmpID equals ei.EmpID into C

  from qei in C.DefaultIfEmpty()

  where qei.EmpID == empid

  select new JCZ03

  {

  idcardno = qei.IDNumber,

  tsno = cityid + qei.StaID.Substring(4, 2),

  personname = qei.EmpName,

  culturegrade = qei.Educational,

  workdate = qei.DivisionTime,

  //1站长 2技术负责人 3质量负责人 4外检员 5登录员 6驾控员 7尾气操作员 8其他

  jobduty = q.RoleID == "130100002" ? "5" : q.RoleID == "130100003" ? "7" : q.RoleID == "130100004" ? "7" : q.RoleID == "130100005" ? "6" : q.RoleID == "130100006" ? "1" : "8",

  //状态(0:未审;1:在岗;2:离职,3 锁止)

  state = qei.EmpState == "0" ? "1" : "2"

  };

  List jcz033 = query.ToList();

  h.organ = Properties.Settings.Default.JKBH;

  h.jkxlh = Properties.Settings.Default.JKXLH;

  h.jkid = "JCZ03";

  b.vehispara = jcz033[0];

  r.head = h;

  r.body = b;

  strxml = Other.XmlSerialize(r);

  Other.WriteLwLog(strxml);//打印发送日志

  restr = System.Web.HttpUtility.UrlDecode(vsc.write(strxml));

  Other.WriteLwLog(restr,false);//打印接受日志

  if (!restr.Contains(@""))

  {

  evl += 1;

  }

  if (evl == 0)

  {

  var query3 = from item in det.Peo_EmployeeInfo

  where item.StaID == orgid && item.EmpID == empid

  select item;

  foreach (var item in query3)

  {

  item.SCBZ = "O";

  }

  det.SubmitChanges();

  upcount += 1;//上传数量 成功上传一条就自增1

  }

  else

  {

  var query3 = from item in det.Peo_EmployeeInfo

  where item.StaID == orgid && item.EmpID == empid

  select item;

  foreach (var item in query3)

  {

  item.SCBZ = "E";

  }

  det.SubmitChanges();

  }

  }

  MessageBox.Show(string.Format("选中{0}条数据,成功上传{1}条!", sumcout, upcount), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

  }

  }