C#的DevExpress实现进度条

 if (gridView8.RowCount == 0)
            {
                XtraMessageBox.Show("没有图书数据不能保存!","提示");
                return;
            }
            //异步执行代码

            if (this.IsHandleCreated) //指示是否关联句柄
            {
                this.BeginInvoke(new EventHandler(delegate
                {
                    string resultes = sqlcmd.TestPing();
                    if (resultes.Equals("1"))
                    {
                        //网络连接成功
                        bool con_falg = sqlcmd.TestConnection(sqlcmd.mysql);
                        if (con_falg)
                        {
                            //true连接数据库成功
                            //关闭数据库
                            Close_DB();
                            //打开数据库
                            comm = Connect_DB();
                            if (gridView8.RowCount > 0)
                            {
                                string info1 = "";
                                Int32[] selectedRowHandles = gridView8.GetSelectedRows();//获取选中的行
                                if (selectedRowHandles.Length > 0)
                                {
                                    #region 进度条
                                    progressBarControl1.Visible = true;
                                    //设置一个最小值
                                    progressBarControl1.Properties.Minimum = 0;//开始值
                                    //设置一个最大值
                                    progressBarControl1.Properties.Maximum = selectedRowHandles.Length;//进度条最大值
                                    //设置步长,即每次增加的数
                                    progressBarControl1.Properties.Step = 1;
                                    //设置进度条的样式
                                    progressBarControl1.Properties.ProgressViewStyle = DevExpress.XtraEditors.Controls.ProgressViewStyle.Solid;
                                    //当前值
                                    progressBarControl1.Position = 0;
                                    //是否显示进度数据
                                    progressBarControl1.Properties.ShowTitle = true;
                                    //是否显示百分比
                                    progressBarControl1.Properties.PercentView = true;

                                    #endregion
                                    //int rowCount = 100;
                                    //for (int i = 0; i < rowCount; i++)
                                    //{
                                    //    Application.DoEvents();
                                    //    progressBarControl1.Position += 1;//进度值加1
                                    //}

                                    for (int i = 0; i < selectedRowHandles.Length; i++)
                                    {
                                        int selectedRowHandle = selectedRowHandles[i];

                                        bool isExist = false;
                                        string isbn = this.gridView8.GetRowCellValue(selectedRowHandle, "书号").ToString();
                                        string bookname = this.gridView8.GetRowCellValue(selectedRowHandle, "书名").ToString();
                                        string locals = this.gridView8.GetRowCellValue(selectedRowHandle, "架号").ToString();
                                        string rfid = this.gridView8.GetRowCellValue(selectedRowHandle, "芯片码").ToString();

                                        string credate = DateTime.Now.ToShortDateString();

                                        //判断书号是否存在
                                        string sql_info = "select isbn,bookname from bookinfo where isbn='" + isbn + "'";

                                        DataTable data_info = comm.SqlseTable(sql_info);
                                        if (data_info != null)
                                        {
                                            if (data_info.Rows.Count > 0)
                                            {

                                            }
                                            else
                                            {
                                               
                                                Application.DoEvents();
                                                progressBarControl1.Position += 1;
                                                //不存在
                                                listBoxControl_BookInfoManagement.Items.Insert(0, "添加图书 " + bookname + "失败,该书号不存在");
                                                continue;

                                            }
                                        }
                                        else
                                        {
                                            Application.DoEvents();
                                            progressBarControl1.Position += 1;
                                            //不存在
                                            listBoxControl_BookInfoManagement.Items.Insert(0, "添加图书 " + bookname + "失败,该书号不存在");
                                            continue;
                                        }


                                        string sql2 = "select isbn,bookname,rfid,locals " +
                                       "from localbook where rfid='" + rfid + "'";

                                        DataTable dataTable = comm.SqlseTable(sql2);

                                        if (dataTable != null)
                                        {
                                            if (dataTable.Rows.Count > 0)
                                            {
                                                // XtraMessageBox.Show("该书号已存在");
                                                listBoxControl_BookInfoManagement.Items.Insert(0, "保存失败:" + rfid + "该芯片码已存在" + " " + bookname + " ");
                                                isExist = true;
                                                Application.DoEvents();
                                                progressBarControl1.Position += 1;
                                            }
                                        }


                                        if (isExist == false)
                                        {


                                            string sql =
                                           string.Format(
                                               "INSERT INTO `borrowsys`.`localbook` (`isbn`, `bookname`, `locals`,  `rfid`,`credate`) values ('{0}','{1}','{2}','{3}','{4}')",
                                               isbn, bookname, locals, rfid, credate);
                                            info1 = comm.sqlupdate(sql);
                                            if (info1 == "1")
                                            {
                                                Application.DoEvents();
                                                progressBarControl1.Position += 1;
                                                listBoxControl_BookInfoManagement.Items.Insert(0, "添加图书成功" + " " + bookname + " " + info1);
                                            }
                                            else
                                            {
                                                Application.DoEvents();
                                                progressBarControl1.Position += 1;
                                                listBoxControl_BookInfoManagement.Items.Insert(0, "添加图书失败" + " " + bookname + " " + info1);
                                            }
                                           
                                        }

                                    }

                                }
                                else
                                {
                                    XtraMessageBox.Show("没有勾选图书信息行,请勾选要保存的图书信息行");
                                }

                            }
                            //关闭数据库
                            Close_DB();
                        }
                        else
                        {
                            //连接数据库失败.请检查数据库是否正常工作
                            XtraMessageBox.Show("连接数据库失败.请检查数据库是否正常工作", "提示");
                        }
                    }
                    else if (resultes.Equals("-1"))
                    {

                        //网络连接超时
                        XtraMessageBox.Show("网络连接超时", "提示");
                    }
                    else if (resultes.Equals("0"))
                    {
                        //网络连接有误,请检查网线是否正确连接
                        XtraMessageBox.Show("网络连接有误,请检查网线是否正确连接", "提示");
                    }

                }));

            }
            //异步执行代码
           

 

你可能感兴趣的:(C#窗体)