dgvHelper,xmlHelper,inputbox(仿vba),Evaluate(vba中函数应用于C#)

DataGridView与Excel公式的C#应用
本文介绍了DataGridView控件的使用技巧,包括如何通过DataTable进行数据操作并更新DataGridView显示,探讨了DataGridView当前行选取的实际考量。此外,还提供了一个自定义的InputBox类用于数据输入,并展示了如何在C#中利用Excel VBA的Evaluate方法计算数学表达式。

1.在dgv中进行增加、删除、修改行时应针对dt进行操作,然后刷新xml

2.datagridview中的currentrow未必就是当前选中行

3.inputbox类

string sDescription = InputBox.ShowInputBox("请输入表达式的说明!");
    public class InputBox : System.Windows.Forms.Form
    {
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox tbData;
        private System.Windows.Forms.Button btnOK;
        private System.Windows.Forms.Button btnCancel;
        private System.Windows.Forms.ToolTip ttpShow;
        private System.ComponentModel.Container components = null;
        private InputBox()
        {
            InitializeComponent();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }

            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.label1 = new System.Windows.Forms.Label();
            this.tbData = new System.Windows.Forms.TextBox();
            this.btnOK = new System.Windows.Forms.Button();
            this.btnCancel = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(13, 13);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(65, 12);
            this.label1.TabIndex = 0;
            this.label1.Text = "在此输入:";
            // 
            // tbData
            // 
            this.tbData.Location = new System.Drawing.Point(84, 10);
            this.tbData.Name = "tbData";
            this.tbData.Size = new System.Drawing.Size(471, 21);
            this.tbData.TabIndex = 1;
            // 
            // btnOK
            // 
            this.btnOK.Location = new System.Drawing.Point(146, 40);
            this.btnOK.Name = "btnOK";
            this.btnOK.Size = new System.Drawing.Size(75, 23);
            this.btnOK.TabIndex = 2;
            this.btnOK.Text = "确定";
            this.btnOK.UseVisualStyleBackColor = true;
            this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
            // 
            // btnCancel
            // 
            this.btnCancel.Location = new System.Drawing.Point(373, 40);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new System.Drawing.Size(75, 23);
            this.btnCancel.TabIndex = 2;
            this.btnCancel.Text = "取消";
            this.btnCancel.UseVisualStyleBackColor = true;
            this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
            // 
            // InputBox
            // 

            this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
            this.ClientSize = new System.Drawing.Size(567, 75);
            this.ControlBox = false;
            this.Controls.Add(this.btnCancel);
            this.Controls.Add(this.btnOK);
            this.Controls.Add(this.tbData);
            this.Controls.Add(this.label1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.Name = "InputBox";
            this.Text = "InputBox";
            this.ResumeLayout(false);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// 显示输入框
        /// </summary>
        /// <param name="sHeadText"></param>
        /// <param name="sHistoryText"></param>
        /// <returns></returns>
        public static string ShowInputBox(string sHeadText, string sHistoryText="")
        {
            InputBox inputbox = new InputBox();
            inputbox.StartPosition = FormStartPosition.CenterParent;
            inputbox.Text = sHeadText;
            if (sHistoryText!="")
            {
                inputbox.tbData.Text = sHistoryText;
            }
            inputbox.ShowDialog();
            return inputbox.tbData.Text;
        }
    }

 

4.按excel vba中的公式evaluate计算

    /// <summary>
    /// 利用Excel中VBA的Evaluate方法对字符串进行计算
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static double Cal(this string str)
    {
      var app = new Excel.Application();
      Excel.Workbook workbook = app.Workbooks.Add();
      Excel.Worksheet worksheet = workbook.Worksheets.Add() as Excel.Worksheet;
      var result = worksheet.Evaluate(str);
      app.DisplayAlerts = false;
      app.Quit();
      double data = double.Parse(result.ToString());
      return data;
    }

5.上项中错误返回的意义

      //-2146826281 =#DIV / 0!
      //-2146826246 - #N / A
      //- 2146826259 =#NAME?
      //-2146826288 =#NULL!
      //-2146826252 =#NUM!
      //-2146826265 =#REF! < -你的!
      //-2146826273 = VALUE!

转载于:https://www.cnblogs.com/swtool/p/7691339.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值