3
回答
C#语言关于使用winform执行存储过程后如何刷新当前窗体?
终于搞明白,存储TCO原来是这样算的>>>   

本人是C#初学者,现在有个问题是当前窗体点击按钮执行存储过程后,当前窗体没有刷新,需要在当前代码的基础上加一些代码,使得存储过程执行后,刷新当前窗体,请问怎么做?

点击下图按钮,会调用方法btnDecideActualData_Click()去执行代码中167行存储过程Do1,请问如何修改代码能让执行这个存储过程后刷新当前这个窗体?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MY.BLL;
using MY.Common;

namespace MY
{

    {   
        #region
        private string connPCDB = null;
        private CommonPara cp = new CommonPara();
        private string flag = "";//初始化被选中的记录的标志位空
        private int checked_count = 0;//计算被选中的checkbox的数量
        private int bin = -1;
        FarPoint.Win.BevelBorder bevelbrdr = new FarPoint.Win.BevelBorder(FarPoint.Win.BevelBorderType.Raised, Color.Gray, Color.Gray);//设置灰色边框
        #endregion

        private delegate bool DoWorkDelegate();

        public frmDecideActualData()
        {
            InitializeComponent();
            this.loadingPanel1.OnRotateStateChanged += new LoadingPanel.RotateStateChangedHandler(loadingPanel1_OnRotateStateChanged);      
        }

        #region 
        
        private void frmDecideActualData_Load(object sender, EventArgs e)
        {   
            try
            {
                connPCDB = cp.GetConString();
                SqlHelper sh = new SqlHelper(connPCDB);
                sh.Param = sh.GetNewParam();
                sh.Param.AddParam("@w", SqlDbType.Int, 0);
                DataSet ds = sh.ExecuteDatasetSP("Get1", ref sh.Param, 3000);
                setSpreadData(ds.Tables[0]);
                for (int i = 0; i < sprdMain.Sheets[0].Rows.Count; i++)//初始化时将不能选的checkbox设置为不可用并添加灰色边框
                {
                    int col7_value = int.Parse(sprdMain.Sheets[0].Cells[i, 6].Value.ToString());//第7列
                    if (col7_value == 1)//如果是1则将checkbox边框设置为灰色
                    {
                        sprdMain.Sheets[0].Cells[i, 4].Locked = true;//将checkbox设置为不可用
                        sprdMain.Sheets[0].Cells[i, 4].Border = bevelbrdr;//给checkbox所在的单元格设置边框,标志这个单元格的checkbox是不可以使用的状态

                    }
                }
            }
            catch (Exception)
            {
 
            }
        }
        #endregion

        #region spread
        private void setSpreadData(DataTable spdt)
        {
            if (this.sprdMain.ActiveSheet.RowCount > 0)
            {
                this.sprdMain.ActiveSheet.Rows.Count = 0;
            }
            this.sprdMain.ActiveSheet.Rows.Count = spdt.Rows.Count;

            DataRowCollection rows = spdt.Rows;
            int rowCount = rows.Count;
            int nowRow = 0;
            for (int i = 0; i < rowCount; i++)
            {
                sprdMain.ActiveSheet.Cells[nowRow, 0].Value = rows[i]["A"].ToString();
                sprdMain.ActiveSheet.Cells[nowRow, 1].Value = rows[i]["B"].ToString();
                sprdMain.ActiveSheet.Cells[nowRow, 2].Value = rows[i]["C"].ToString();
                sprdMain.ActiveSheet.Cells[nowRow, 3].Value = rows[i]["D"].ToString();
                sprdMain.ActiveSheet.Cells[nowRow, 4].Value = rows[i]["E"].ToString();
                sprdMain.ActiveSheet.Cells[nowRow, 5].Value = rows[i]["F"].ToString();
                sprdMain.ActiveSheet.Cells[nowRow, 6].Value = rows[i]["G"].ToString();

                nowRow++;
            }
        }
        #endregion

        #region 
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        void loadingPanel1_OnRotateStateChanged()
        {
            this.Invoke(new Action(delegate()
            {
                this.sprdMain.Visible = !this.sprdMain.Visible;
                this.btnDecideActualData.Enabled = !this.btnDecideActualData.Enabled;
                this.btnExit.Enabled = !this.btnExit.Enabled;

            }));
        }

        private bool DoWork()
        {
            System.Threading.Thread.Sleep(10000);
            return true;
        }

        private void DoWorkCallBack(IAsyncResult ar)
        {
            AsyncResult result = (AsyncResult)ar;
            DoWorkDelegate caller = (DoWorkDelegate)result.AsyncDelegate;
            bool bResult = caller.EndInvoke(ar);

            this.loadingPanel1.Stop();
        }

        private void btnDecideActualData_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("do it", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
            {
                return;
            }
            else
            { //点击的是"OK"按钮
                int count = 0;//计算被选择的复选框数量
                string status = "";
                for (int i = 0; i < sprdMain.Sheets[0].Rows.Count; i++)
                {
                    status = sprdMain.Sheets[0].Cells[i, 4].Value.ToString();//复选框是否被选中
                    if (status == "True")//"True"为选中,"0"为未选中。
                    {
                        count++;
                    }
                }
                if (count == 0)
                {

                    return;
                }
                else if (count > 1)
                {
                    
                    return;
                }
                else//count == 1
                {
                    //
                    DoWorkDelegate doWorkDelegate = this.DoWork;
                    doWorkDelegate.BeginInvoke(new AsyncCallback(DoWorkCallBack), null);
                    this.loadingPanel1.Start();
                    //
                    try
                    {
                        connPCDB = cp.GetConString();
                        SqlHelper sh = new SqlHelper(connPCDB);
                        sh.Param = sh.GetNewParam();
                        sh.Param.AddParam("@a", SqlDbType.Int, -1);
                        sh.Param.AddParam("@b", SqlDbType.Int, bin);
                        DataSet ds = sh.ExecuteDatasetSP("Do1 ", ref sh.Param, 3000);

                    }
                    catch (Exception)
                    {

                    }

                }

            }
        }

        private void sprdMain_ButtonClicked(object sender, FarPoint.Win.Spread.EditorNotifyEventArgs e)
        {
            checked_count = 0;
            for (int i = 0; i < sprdMain.Sheets[0].Rows.Count; i++)
            {
                if (checked_count <= 1)
                {
                    flag = sprdMain.Sheets[0].Cells[i, 4].Value.ToString();//复选框是否被选中
                    if (flag == "True")//"True"为选中,"0"为未选中。
                    {
                        int col7_value = int.Parse(sprdMain.Sheets[0].Cells[i, 6].Value.ToString());//第7列
                        if (col7_value == 0)//如果是0则checkbox可用
                        {
                            bin = int.Parse(sprdMain.Sheets[0].Cells[i, 0].Value.ToString());
                            checked_count++;
                        }
                    }
                }
                else
                {
                    
                    checked_count = 0;
                    break;
                }
            }
        }     
    }

  
}



<无标签>
举报

引用来自“张亦俊”的评论

重新调用一下你这里边的setSpreadData?

或者说把this.sprdMain清空一下再重新调用setSpreadData?

桌面应用不比web,没有直接刷新的功能哟

我这样写了,好用了,谢谢!208-232行是把load方法封装了一下,然后174行重新调用。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MY.BLL;
using MY.Common;
 
namespace MY
{
 
    {   
        #region
        private string connPCDB = null;
        private CommonPara cp = new CommonPara();
        private string flag = "";//初始化被选中的记录的标志位空
        private int checked_count = 0;//计算被选中的checkbox的数量
        private int bin = -1;
        FarPoint.Win.BevelBorder bevelbrdr = new FarPoint.Win.BevelBorder(FarPoint.Win.BevelBorderType.Raised, Color.Gray, Color.Gray);//设置灰色边框
        #endregion
 
        private delegate bool DoWorkDelegate();
 
        public frmDecideActualData()
        {
            InitializeComponent();
            this.loadingPanel1.OnRotateStateChanged += new LoadingPanel.RotateStateChangedHandler(loadingPanel1_OnRotateStateChanged);      
        }
 
        #region 
         
        private void frmDecideActualData_Load(object sender, EventArgs e)
        {   
            try
            {
                connPCDB = cp.GetConString();
                SqlHelper sh = new SqlHelper(connPCDB);
                sh.Param = sh.GetNewParam();
                sh.Param.AddParam("@w", SqlDbType.Int, 0);
                DataSet ds = sh.ExecuteDatasetSP("Get1", ref sh.Param, 3000);
                setSpreadData(ds.Tables0);
                for (int i = 0; i < sprdMain.Sheets0.Rows.Count; i++)//初始化时将不能选的checkbox设置为不可用并添加灰色边框
                {
                    int col7_value = int.Parse(sprdMain.Sheets0.Cells[i, 6].Value.ToString());//第7列
                    if (col7_value == 1)//如果是1则将checkbox边框设置为灰色
                    {
                        sprdMain.Sheets0.Cells[i, 4].Locked = true;//将checkbox设置为不可用
                        sprdMain.Sheets0.Cells[i, 4].Border = bevelbrdr;//给checkbox所在的单元格设置边框,标志这个单元格的checkbox是不可以使用的状态
 
                    }
                }
            }
            catch (Exception)
            {
  
            }
        }
        #endregion
 
        #region spread
        private void setSpreadData(DataTable spdt)
        {
            if (this.sprdMain.ActiveSheet.RowCount > 0)
            {
                this.sprdMain.ActiveSheet.Rows.Count = 0;
            }
            this.sprdMain.ActiveSheet.Rows.Count = spdt.Rows.Count;
 
            DataRowCollection rows = spdt.Rows;
            int rowCount = rows.Count;
            int nowRow = 0;
            for (int i = 0; i < rowCount; i++)
            {
                sprdMain.ActiveSheet.Cells[nowRow, 0].Value = rows[i]["A"].ToString();
                sprdMain.ActiveSheet.Cells[nowRow, 1].Value = rows[i]["B"].ToString();
                sprdMain.ActiveSheet.Cells[nowRow, 2].Value = rows[i]["C"].ToString();
                sprdMain.ActiveSheet.Cells[nowRow, 3].Value = rows[i]["D"].ToString();
                sprdMain.ActiveSheet.Cells[nowRow, 4].Value = rows[i]["E"].ToString();
                sprdMain.ActiveSheet.Cells[nowRow, 5].Value = rows[i]["F"].ToString();
                sprdMain.ActiveSheet.Cells[nowRow, 6].Value = rows[i]["G"].ToString();
 
                nowRow++;
            }
        }
        #endregion
 
        #region 
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion
 
        void loadingPanel1_OnRotateStateChanged()
        {
            this.Invoke(new Action(delegate()
            {
                this.sprdMain.Visible = !this.sprdMain.Visible;
                this.btnDecideActualData.Enabled = !this.btnDecideActualData.Enabled;
                this.btnExit.Enabled = !this.btnExit.Enabled;
 
            }));
        }
 
        private bool DoWork()
        {
            System.Threading.Thread.Sleep(10000);
            return true;
        }
 
        private void DoWorkCallBack(IAsyncResult ar)
        {
            AsyncResult result = (AsyncResult)ar;
            DoWorkDelegate caller = (DoWorkDelegate)result.AsyncDelegate;
            bool bResult = caller.EndInvoke(ar);
 
            this.loadingPanel1.Stop();
        }
 
        private void btnDecideActualData_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("do it", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
            {
                return;
            }
            else
            { //点击的是"OK"按钮
                int count = 0;//计算被选择的复选框数量
                string status = "";
                for (int i = 0; i < sprdMain.Sheets0.Rows.Count; i++)
                {
                    status = sprdMain.Sheets[0].Cells[i, 4].Value.ToString();//复选框是否被选中
                    if (status == "True")//"True"为选中,"0"为未选中。
                    {
                        count++;
                    }
                }
                if (count == 0)
                {
 
                    return;
                }
                else if (count > 1)
                {
                     
                    return;
                }
                else//count == 1
                {
                    //
                    DoWorkDelegate doWorkDelegate = this.DoWork;
                    doWorkDelegate.BeginInvoke(new AsyncCallback(DoWorkCallBack), null);
                    this.loadingPanel1.Start();
                    //
                    try
                    {
                        connPCDB = cp.GetConString();
                        SqlHelper sh = new SqlHelper(connPCDB);
                        sh.Param = sh.GetNewParam();
                        sh.Param.AddParam("@a", SqlDbType.Int, -1);
                        sh.Param.AddParam("@b", SqlDbType.Int, bin);
                        DataSet ds = sh.ExecuteDatasetSP("Do1 ", ref sh.Param, 3000);
 
                    }
                    catch (Exception)
                    {
 
                    }
 					frmDecideActualData_Common_Load();//执行完存储过程后调用该方法重新查询数据
                }
 
            }
        }
 
        private void sprdMain_ButtonClicked(object sender, FarPoint.Win.Spread.EditorNotifyEventArgs e)
        {
            checked_count = 0;
            for (int i = 0; i < sprdMain.Sheets[0].Rows.Count; i++)
            {
                if (checked_count <= 1)
                {
                    flag = sprdMain.Sheets[0].Cells[i, 4].Value.ToString();//复选框是否被选中
                    if (flag == "True")//"True"为选中,"0"为未选中。
                    {
                        int col7_value = int.Parse(sprdMain.Sheets[0].Cells[i, 6].Value.ToString());//第7列
                        if (col7_value == 0)//如果是0则checkbox可用
                        {
                            bin = int.Parse(sprdMain.Sheets[0].Cells[i, 0].Value.ToString());
                            checked_count++;
                        }
                    }
                }
                else
                {
                     
                    checked_count = 0;
                    break;
                }
            }
        }
			
			
			private void frmDecideActualData_Common_Load() {//重新加载窗体
            try
            {
                connPCDB = cp.GetConString();
                SqlHelper sh = new SqlHelper(connPCDB);
                sh.Param = sh.GetNewParam();
                sh.Param.AddParam("@DISP_TYPE", SqlDbType.Int, 0);
                DataSet ds = sh.ExecuteDatasetSP("GetProductionTableDataForDecideActual", ref sh.Param, 3000);
                setSpreadData(ds.Tables[0]);
                for (int i = 0; i < sprdMain.Sheets[0].Rows.Count; i++)//初始化时将不能选的checkbox设置为不可用并添加灰色边框
                {
                    int col7_value = int.Parse(sprdMain.Sheets[0].Cells[i, 6].Value.ToString());//第7列出荷確定済フラグ的值
                    if (col7_value == 1)//如果是1则将checkbox边框设置为灰色
                    {
                        sprdMain.Sheets[0].Cells[i, 4].Locked = true;//将checkbox设置为不可用
                        sprdMain.Sheets[0].Cells[i, 4].Border = bevelbrdr;//给checkbox所在的单元格设置边框,标志这个单元格的checkbox是不可以使用的状态

                    }
                }
            }
            catch (Exception)
            {
                
            }
        }     
    }
 
}




重新调用一下你这里边的setSpreadData?

或者说把this.sprdMain清空一下再重新调用setSpreadData?

桌面应用不比web,没有直接刷新的功能哟

顶部