在数据表中使用UDF进行datagridview

时间:2013-04-03 14:42:48

标签: c# sql winforms datatable

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

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

所以,我认为我可以将计算字段与DataTable对象一起发送,或者我需要将计算方法挂钩到Windows窗体/ 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个值。我加载表单时没有计算出来。

2 个答案:

答案 0 :(得分:0)

在load form事件上以及将datatable绑定到datagrid之后 然后 通过编码更改网格中所需的值(此更改fire cellvalue更改事件) 然后返回你改变的旧值 如果你不两次触发此事件,你可以定义bool参数atLoad 并在加载表单时将其设置为true,然后在加载完成时将其设置为false 在datagridWorkorderPartItems_CellValueChanged事件中使用此参数 if(atLoad == true)返回; 一旦事件什么都不做就改变值 在两次atLoad = false事件计算你的wati

答案 1 :(得分:-1)

FormulaEngine是一个.NET程序集,可让您为应用程序添加公式支持。它负责解析和评估公式,跟踪它们的依赖关系,并以自然顺序重新计算。

看看下面的文章。

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