我将 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 个值。当我加载表格时,这些没有得到计算。