0

我在 VS2008 环境中使用 C#。

我有一个名为 frmStrategy5YrPlan 的表单,其中有一个数据网格视图,其中填充了数据,然后允许用户键入新的预测数据。完成后,用户将单击一个按钮来上传他们输入的新数据,我希望显示另一个表单,要求他们确认他们想要这样做(因为该过程会删除所有旧数据并替换为已输入)。

我总是被表单欺骗,但本质上我希望我的初始表单加载第二个表单,评估用户是否选择确认加载数据(或不),然后使用初始表单中的代码处理数据。

这是我的初始表格,今天可以正常工作而无需调用第二种表格(我正在询问)。下面是初学者对第二种形式的代码和我添加的按钮的工作,只是试图让两种形式相互交谈。在另一个部分调用表单时,它总是让我感到困惑。我对 MDIParents 不太熟悉,但我在这里尝试过,我的初始表单没有设置为一个,所以我不确定这是否是解决方案。

public void btnUploadNewStrategy5YrPlan_Click(object sender, EventArgs e)
        {
            //this.ConfirmForm.Text = "DSC_0 Staged";
            //this.ConfirmForm.EnableReport(false);
            //this.ConfirmForm.Type = frmConfirmForecastUpload.LoadType.DSC_0;
            //this.ConfirmForm.MdiParent = this;
            this.ConfirmForm.Top = this.Height / 3 - this.ConfirmForm.Height / 2;
            this.ConfirmForm.Left = this.Width / 2 - this.ConfirmForm.Width / 2;
            this.ConfirmForm.Show();
            Cursor.Current = Cursors.WaitCursor;

            SqlCommand cmd = null;
            SqlDataReader dr = null;
            StringBuilder sql = new StringBuilder();

            try
            {
                var dtForecast = new DataTable();
                dtForecast.Columns.Add("Unit");
                dtForecast.Columns.Add("Year");
                dtForecast.Columns.Add("Period");
                dtForecast.Columns.Add("Acct");
                dtForecast.Columns.Add("Descr");
                dtForecast.Columns.Add("DEFERRAL_TYPE");
                dtForecast.Columns.Add("NDC_Indicator");
                dtForecast.Columns.Add("Mgmt Cd");
                dtForecast.Columns.Add("Prod");
                dtForecast.Columns.Add("Node");
                dtForecast.Columns.Add("Curve_Family");
                dtForecast.Columns.Add("Sum Amount");
                dtForecast.Columns.Add("Base Curr");
                dtForecast.Columns.Add("Ledger");

                for (int ii = 0; ii < grd1.Rows.Count; ii++)
                {
                    int myCountryKey = int.Parse(grd1.Rows[ii].Cells["colCountryKey"].FormattedValue.ToString());
                    int myCurrencyKey = int.Parse(grd1.Rows[ii].Cells["colCurrencyKey"].FormattedValue.ToString());
                    string myCurrencyCode = grd1.Rows[ii].Cells["colCurr"].FormattedValue.ToString();
                    int myDeferralTypeKey = int.Parse(grd1.Rows[ii].Cells["colDeferralTypeKey"].FormattedValue.ToString());
                    string myDeferralTypeCode = grd1.Rows[ii].Cells["colDeferralType"].FormattedValue.ToString();
                    long myAccount = 6018110000;
                    string myAcctDesc = "";
                    string myCurveFamily = "AH_DM"; //hard coded to AH_DM for now since only DM is in AMORT
                    myDataRegionKey = int.Parse(grd1.Rows[ii].Cells["colRegionKey"].FormattedValue.ToString());
                    myNodeKey = int.Parse(grd1.Rows[ii].Cells["colNodeKey"].FormattedValue.ToString());
                    string myNodeCode = grd1.Rows[ii].Cells["colNodeCode"].FormattedValue.ToString();
                    mySubNodeKeyString = grd1.Rows[ii].Cells["colSubNodeKey"].FormattedValue.ToString();

                    int mySubNodeKey = int.TryParse(mySubNodeKeyString, out mySubNodeKey) ? mySubNodeKey : 0;


                    //Run query to get BU/MCC defaults for this countryKey
                    cmd = util.SqlConn.CreateCommand();
                    cmd.CommandTimeout = 600;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "proc_get_country_defaults";
                    cmd.Parameters.Add("@node_key", SqlDbType.VarChar).Value = myNodeKey;
                    cmd.Parameters.Add("@country_key", SqlDbType.VarChar).Value = myCountryKey;
                    cmd.Parameters.Add("@division_key", SqlDbType.VarChar).Value = myDivisionKey;
                    dr = cmd.ExecuteReader();

                    dr.Read();
                    int myMCC = (int.Parse(dr["management_code"].ToString()));
                    int myBusinessUnit = (int.Parse(dr["business_unit_code"].ToString()));
                    dr.Close();

                    //Run query to get product defaults for this NodeKey
                    cmd = util.SqlConn.CreateCommand();
                    cmd.CommandTimeout = 600;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "proc_get_node_defaults";
                    cmd.Parameters.Add("@node_key", SqlDbType.VarChar).Value = myNodeKey;
                    dr = cmd.ExecuteReader();

                    dr.Read();
                    int myProductKey = (int.Parse(dr["product_key"].ToString()));                    
                    dr.Close();

                    //Run query to get product defaults for this SubNodeKey
                    cmd = util.SqlConn.CreateCommand();
                    cmd.CommandTimeout = 600;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "proc_get_sub_node_defaults";
                    cmd.Parameters.Add("@sub_node_key", SqlDbType.VarChar).Value = mySubNodeKey;
                    dr = cmd.ExecuteReader();

                    //Only overwrite the product key with subnode default if
                    //subnodekey is not null and region is Latin America
                    if (mySubNodeKey > 0 && myDataRegionKey == 100001)
                    {
                        dr.Read();
                        myProductKey = (int.Parse(dr["product_key"].ToString()));
                        dr.Close();
                    }
                    else
                    {
                        dr.Close();
                    }          

                    //Loop through the 5 years
                    for (int jj = 1; jj < 6; jj++)
                    {
                        if (myDataYear > myMostRecentActualYear)
                        {

                            string myYearCount = jj.ToString().PadLeft(2, '0');
                            string myColName = "col" + myYearCount;
                            double mySumAmt = Convert.ToDouble(grd1.Rows[ii].Cells[myColName].Value) * 1000 / 12;  //Convert annual amount to monthly

                            if (mySumAmt != 0)
                            {

                                //Loop through 12 months
                                for (int nn = 1; nn < 13; nn++)
                                {
                                    int myDataYearToWrite = myDataYear - 1 + jj;

                                    DataRow _fcst = dtForecast.NewRow();
                                    _fcst["Unit"] = myBusinessUnit;
                                    _fcst["Year"] = myDataYearToWrite;
                                    _fcst["Period"] = nn;
                                    _fcst["Acct"] = myAccount;
                                    _fcst["Descr"] = myAcctDesc;
                                    _fcst["DEFERRAL_TYPE"] = myDeferralTypeCode;
                                    _fcst["NDC_Indicator"] = "";
                                    _fcst["Mgmt Cd"] = myMCC;
                                    _fcst["Prod"] = myProductKey;
                                    _fcst["Node"] = myNodeCode;
                                    _fcst["Curve_Family"] = myCurveFamily;
                                    _fcst["Sum Amount"] = mySumAmt;
                                    _fcst["Base Curr"] = myCurrencyCode;
                                    _fcst["Ledger"] = "CORE";

                                    dtForecast.Rows.Add(_fcst);
                                }
                            }
                        }
                    }                                     

                }

                //Export data table to Excel file
                Excel.Application xlApp;
                Excel.Workbook xlWorkBook;
                Excel.Worksheet xlWorkSheet;
                object misValue = System.Reflection.Missing.Value;

                xlApp = new Excel.ApplicationClass();

                xlWorkBook = xlApp.Workbooks.Add(misValue);

                xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

                int x = 0;
                int y = 0;

                //// storing header part in Excel 
                for (int q = 1; q < dtForecast.Columns.Count + 1; q++)
                {
                    xlWorkSheet.Cells[1, q] = dtForecast.Columns[q - 1].ToString().ToUpper() + "\t";
                    string myHeader = dtForecast.Columns[q - 1].ToString().ToUpper();
                }

                // storing grid data part in Excel 
                for (x = 0; x <= dtForecast.Rows.Count - 1; x++)
                {
                    for (y = 0; y <= dtForecast.Columns.Count - 1; y++)
                    {
                        string myData = Convert.ToString(dtForecast.Rows[x][y]);

                        xlWorkSheet.Cells[x + 2, y + 1] = myData;
                    }
                }

                string tempPath = Path.GetTempPath();  //System.IO.Path.GetTempPath();
                string fileName = Guid.NewGuid().ToString(); //+ extension;
                myFileSave = tempPath + fileName;

                xlWorkBook.SaveAs(myFileSave, Excel.XlFileFormat.xlWorkbookDefault, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
                xlWorkBook.Close(true, misValue, misValue);

                xlApp.Quit();

                //Kill Excel objects
                releaseObject(xlWorkSheet);
                releaseObject(xlWorkBook);
                releaseObject(xlApp);

                //MessageBox.Show("Excel file created , you can find the file c:\\AMORT_data.xls");

                //This will import forecast to the zstbl_sol_cost staging table, and validate the data
                ImportStrategy5YrPlan();
                //This will load the data into tbl_cohort and then call Amortizer
                LoadStrategy5YrPlan();

            }

            catch (Exception ex)
            {
                util.LogError(ex);
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            finally
            {
                if (dr != null) dr.Dispose();
                if (cmd != null) cmd.Dispose();
            }


            //dtForecast.Load.Add(theRow);
        }

新形式

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AmortClient
{
    public partial class frmConfirmForecastUpload : Form
    {
        public frmConfirmForecastUpload()
        {
            InitializeComponent();
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            frmStrategy5YrPlan console = (frmStrategy5YrPlan)this.MdiParent;
            Cursor.Current = Cursors.WaitCursor;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {

        }
    }
4

2 回答 2

6

只需使用消息框并在其上设置提示按钮,然后等待成功。

MessageBox.Show("Are you sure?", "Confirm", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
于 2013-04-30T20:14:27.287 回答
0

上面的答案是一个很好的方法,但是如果出于某种原因您可能会显示一些附加信息并且需要的不仅仅是消息框,您可以使用 Form.ShowDialog http://msdn.microsoft.com/en-us/库/system.windows.forms.form.showdialog(v=VS.71).aspx

我在工作,所以没有 VS 向您展示一个工作示例 atm,但如果您需要但基本上只是搜索教程。

当您需要打开第二个表格时,从第一个表格开始

如果(form2.ShowDialog = DialogResult.OK){

}

于 2013-04-30T20:34:43.077 回答