none
Simple column multiplication - How to?

    Question

  • I have a table in access that i populate my dataTable with,

    it has a Shares and a Price column,

     

    I would like to have a new column in another datagrid that says Investment, which Shares * Price.

     

    This is what I have so far, but I am unsure what to do after this.

     

    Also if we could use a simple algorithm that would be great.

     

    dataTable.Columns.Add("Total Invested");

    foreach (DataRow row in dataTable.Rows)

    {

    string shares = row["Shares"].ToString();

    string moneyI = row["OpeningPrice"].ToString();

    int s = int.Parse(shares);

    int m = int.Parse(moneyI);

    int t;

    t = s * m;

    // I am stuck here

    }

     

    So what do I need to do to populate this new column? and also how do I bind it to another datagrid?

    Any help would be much appreciated thanks

    Wednesday, April 18, 2007 1:42 AM

All replies

  • Hello,

    You can use Expression Columns.

    Sample from MSDN.

    Code Snippet

    workTable.Columns.Add("Total", typeof(Double));

    workTable.Columns.Add("SalesTax", typeof(Double), "Total * 0.086");

     

    So, in your case:

     

    Code Snippet

    dataTable.Columns.Add("TotalInvested", typeof(Double), "Shares * OpeningPrice");

     

     Happy coding.

     

    Wednesday, April 18, 2007 6:44 AM
  • It is an addition. You could use expressions inside of the SQLs. When you select data from the database you could calculate it inside of SQL like

     

    SELECT ......, Shares * Price AS Total FROM .....

    Wednesday, April 18, 2007 10:30 AM
  • Hey,

     

    Thank you very much for that! That's excellent.

     

    But now I would like to populate another datagrid with some of my columns from my datatable, how do you do that?

    right now I have all the columns in one datagrid this is the code for that and now lets say i want some  in datagrid2 - colums Shares and Price.

     

    string SQLall = "SELECT * FROM Company";

    oleDbDataAdapter1.SelectCommand.CommandText = SQLall;

    dataSet1.Clear();

    oleDbDataAdapter1.Fill(dataSet1, "AllPort");

    dataGrid1.SetDataBinding(dataSet1, "AllPort");

     

    Thanks again!

     

    Wednesday, April 18, 2007 11:06 AM
  • ALSO ONE more thing about multiplication

     

    how would I do it if it was a variable not a column let's say j

     

    because I tried

     

    dataTable.Columns.Add("TotalInvested", typeof(Double), "Shares *" +j+"");

     

    but it didnt work. it just gives me a Null value.

    Is there another method for variables?

     

    Wednesday, April 18, 2007 12:59 PM
  • What kind of variable is j? Are you expecting the values in the column to change as the value of j changes?
    Wednesday, April 18, 2007 8:05 PM
  • Yes that's correct,

     

    j is the totalAmountInvested so it will be changing according to the user's input.

     

    such as

    string j = dataTable.Compute("SUM(totalinvested)", "").ToString();

    ,

     

    and also would you an efficient way of binding just a selected numbers of columns to another datagrid from a datatable?

     

    Thanks Smile

    Wednesday, April 18, 2007 8:38 PM
  • Hi,

     

    In your sample:

     

    dataTable.Columns.Add("TotalInvested", typeof(Double), "Shares *" +j+"");

     

    you can try placing a space after "*". And remove the empty string afterwards.

    Also, try checking if your J variable contains a value coz it will cause an error if J contains an empty string.

     

    dataTable.Columns.Add("TotalInvested", typeof(Double), "Shares * " + j);

     

     

    cheers,

     

    Paul June A. Domag

    Wednesday, April 18, 2007 9:24 PM