I am passing a DataTable object into a Windows Form and
binding the DataTable to a DataGridView. In the DataGridView I allow a user to edit and
add data / rows. I need to display the results of two calculations on the DataGridView
depending on the data that the user enters. The calculations are results of 2 methods
that get called from events on the DataGridView. Those work great during an active edit
or use of the DataGridView.
After a user saves the items I return the
DataTable object to a class that saves the data to the underlying table.
The problem I have is when a user opens the
Windows Form again to edit a record, my calculations do not show because events have not
been fired on the DataGridView that would trigger the respective
methods.
So, I was thinking that I could send
the calculated fields along with the DataTable object, or I need to hook the calculation
methods to more events in the Windows Form /
DataGridView.
If I send the calculated fields
along with the DataTable, that would cause issues with returning the DataTable object
for the database update actions
right?
I would appreciate thoughts
and / or suggestions.
Thanks
I can
post any relevant code if it
helps.
*this code
handles data changes in the grid view
cells*
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);
}
}
*This
code gets the data table I am working
on*
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)
and part.LookupPartMargin(ID) are the 2 values that are calculated when ever a change is
made to a column in the datagridview. THose do not get calculated when I load the
form.
No comments:
Post a Comment