繁体   English   中英

在datatable中使用UDF进行datagridview

[英]using UDF in datatable for datagridview

我将DataTable对象传递到Windows窗体中,并将​​DataTable绑定到DataGridView。 在DataGridView中,我允许用户编辑和添加数据/行。 我需要根据用户输入的数据在DataGridView上显示两次计算的结果。 计算是从DataGridView上的事件调用的2种方法的结果。 在主动编辑或使用DataGridView期间,这些功能非常有用。 用户保存项目后,我将DataTable对象返回到一个将数据保存到基础表的类。

我遇到的问题是,当用户再次打开Windows窗体以编辑记录时,我的计算未显示,因为尚未触发将触发相应方法的DataGridView上未触发事件。

因此,我当时想可以将计算出的字段与DataTable对象一起发送,或者我需要将计算方法与Windows Form / DataGridView中的更多事件挂钩。

如果我将计算出的字段与DataTable一起发送,那会导致为数据库更新操作返回DataTable对象时出现问题,对吗?

我将不胜感激和/或建议。 谢谢

如果有帮助,我可以发布任何相关代码。

* 此代码处理网格视图单元格中的数据更改 *

private void datagridWorkorderPartItems_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    int ID;
    HighlightSaveItems();
    DataGridViewRow row = this.datagridWorkorderPartItems.Rows[e.RowIndex];
    DataGridViewCell cell = row.Cells[e.ColumnIndex];
    if (!DBNull.Value.Equals(row.Cells[3].Value) && !DBNull.Value.Equals(row.Cells[2].Value))
    {
        if (e.ColumnIndex == 2 || e.ColumnIndex == 3)
        {
            decimal price;
            decimal originalprice;
            decimal partmargin;
            Pricing p = new Pricing();
            ID = Convert.ToInt32(row.Cells[3].Value);

            //if price all ready has a value or has been altered, don't change it
            price = p.GetPartItemPrice(ID);
            originalprice = price;
            Parts part = new Parts();
            partmargin = part.LookupPartMargin(ID);
            //now take the price and check against customer price level
            decimal pricelevel;
            decimal invertedpricelevel;
            string level;
            Customers c = new Customers();
            level = c.GetCustomerPartsLevel(_cid);
            Options o = new Options();
            pricelevel = o.GetPartsLevelPercent(level);

            //get proper percent - 100
            if (pricelevel == 1)
            {
                invertedpricelevel = 1;
            }
            else
            {
                invertedpricelevel = 1 - pricelevel; //inverted the percent
            }

            price = price * invertedpricelevel;

            try
            {
                if (e.ColumnIndex == 3) //column 3 is part id
                {
                    if (row.Cells[2].Value == null || row.Cells[2].Value == "0") //qty is null or 0
                    {
                        row.Cells[2].Value = "1"; //set it to assume 1
                        if (row.Cells[4].Value == null)
                        {
                            row.Cells[4].Value = price * 1; //assume the price is price * 1
                            row.Cells[5].Value = originalprice;
                            row.Cells[6].Value = partmargin.ToString("P");
                        }
                    }
                    else
                    {
                        row.Cells[4].Value = price * Convert.ToInt32(row.Cells[2].Value);
                        row.Cells[5].Value = originalprice;
                        row.Cells[6].Value = partmargin.ToString("P");
                    }
                }
                else if (e.ColumnIndex == 2)
                {
                    if (row.Cells[4].Value == null)
                    {
                        row.Cells[4].Value = "0";
                        row.Cells[5].Value = "0";
                        row.Cells[6].Value = "0";
                    }
                    else
                    {
                        row.Cells[4].Value = price * Convert.ToInt32(row.Cells[2].Value);
                        row.Cells[5].Value = originalprice;
                        row.Cells[6].Value = partmargin.ToString("P");
                    }
                }
            }
            catch (Exception m)
            {
                MessageBox.Show("Error: " + m.Message.ToString() + " Source: " + m.Source.ToString() + " " + m.InnerException.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

* 此代码获取我正在处理的数据表 *

public DataTable WorkorderPartItemsTable(int WOID)
{
    ConfigDAL config = new ConfigDAL();
    string connstr = config.GetConnString();
    SqlConnection conn = new SqlConnection(connstr);
    string query;
    Parts part = new Parts();

    query = "SELECT * FROM WorkorderItems WHERE (WorkorderID = @WorkorderID) AND (PartID <> '0')";
    SqlDataAdapter da = new SqlDataAdapter();
    da.SelectCommand = new SqlCommand(query, conn);
    da.SelectCommand.Parameters.AddWithValue("@WorkorderID", WOID);
    DataTable dt = new DataTable();
    conn.Open();
    da.Fill(dt);
    conn.Close();
    return dt;
}

p.GetPartItemPrice(ID)和part.LookupPartMargin(ID)是对datagridview中的列进行更改时要计算的2个值。 加载表单时不会计算这些内容。

在加载表单事件上并将数据表绑定到datagrid之后,通过编码在网格中更改所需的值(此更改触发单元格值更改事件),然后返回更改的旧值(如果不触发两次该事件,则可以定义称为bool参数)它将atLoad设置为true并在加载表单时将其设置为true,然后在加载完成时将其设置为false。如果返回(atLoad == true),请在datagridWorkorderPartItems_CellValueChanged事件中使用此参数; 当第一次更改值时,两次事件都不执行atLoad = false事件时,计算您拥有的内容

FormulaEngine是.NET程序集,使您可以向应用程序添加公式支持。 它负责解析和评估公式,跟踪其依赖性以及以自然顺序重新计算。

看看下面的文章。

http://www.codeproject.com/Articles/17853/Implementing-an-Excel-like-formula-engine

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM