What you could do is:
- Get data from database into data table
- Pick the two lines for Rate and Total from the result
- (If you don't need to display them, remove these lines from the table)
- Do your calculation based on the values in the two rows
- Add a new row to the table manually for the results
EDIT
Maybe I didn't fully understand your data structure. So let's look at some scenarios. If yours isn't in there, please provide more detail on how one row in your data table actually looks like:
Case a: Data is stored within columns, you want the Totals and Amount columns within that row, too
Value1 Value2 Value3 Rate Total Amount
1 5 3 10 ? ?
You could adapt your SQL statement as follows:
SELECT Value1, Value2, Value3, Rate,
(Value1+Value2+Value3) AS Total,
((Value1+Value2+Value3) * Rate) AS Amount
FROM <table>
Alternatively, if you're using a typed dataset in C#, you could use a normal SELECT * ...
statement and add columns with respective expressions (Expression
property - the equivalent to computed columns) manually to the table in your typed dataset.
Case b: Data is stored within rows, you want Total and Amount to be rows, too
RowTitle Value
Value1 1
Value2 5
Value3 2
Rate 10
Now you need to use a for-loop in C# to iterate over all the rows, sum up the values and then create two new rows with RowTitle
"Total" and "Amount" and add them to your table.
EDIT 2
You, so I understand from your second comment that you actually have two tables in your database. One that contains the actual data, and one that contains the Rates. You now select from these tables according to user defined settings and you actually get two tables in your data set in C# - one for that data, one for the Rate.
AFAIK the SQL Server Compact Edition does not allow you to use JOIN
, but that would have been the easiest: select the data from your data table and join it to the "Rates" table, selecting the respective rate according to the user settings. Then you could do the calculation I noted in "Case a" above.
However, I think you can not use JOIN
, so I'd suggest that you get the data into your application (resulting in two separate tables, one for the data, one for the Rate). You modify your data SELECT
statement so that you get an additional column Amount
with default value 0
.
SELECT *, 0 AS Amount FROM <data table>
Then, in C# you iterate over all the records in your data table and calculate the value of Amount
for the record.
private void GetData(...)
{
// Call SELECT statements to fill a dataset with two tables
DataSet set = new DataSet();
....
// set now contains tables[0] (for the data) and tables[1] (for the Rate).
// Get the Rate
int rate = (int)set.Tables[1].Rows[0]["Rate"]; // Assuming that the column is named "Rate"
// Now iterate over all data rows and calculate amount
foreach (DataRow row in set.Tables[0])
{
row["Amount"] = ((int)row["Total"] * rate);
}
// Mark the data set as unmodified
set.AcceptChanges();
}
Note that this code may need some tuning - I've not actually tested it. If the "Rates"-Table contains more than one row and the correct rate needs to be selected depending on other settings, you might consider creating a function that returns the correct row depending on these settings:
private DataRow FindRateRow(DataSet set, <parameters for selecting the correct row>)
{
DataRow[] row = set.Tables[1].Select(<Filter string according to parameters>);
if (row != null && row.Length > 0)
return row[0];
return null;
}
Use int rate = (int)FindRateRow(set, ...)["Rate"];
above.