Click here to Skip to main content
15,898,743 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am new to c#. I am trying to let the application calculate the ["High"]-["Low"] for me when the data is printed on the ListView. Can someone provide some information how to work on it. Thank you.

********************My Project**********************

lv.Columns.Add("Price", 50, HorizontalAlignment.Right);
lv.Columns.Add ( "Date" , 80 , HorizontalAlignment.Right ) ;
lv.Columns.Add("High", 60, HorizontalAlignment.Right);
lv.Columns.Add("Low", 60, HorizontalAlignment.Right);
lv.Columns.Add("High-Low", 60, HorizontalAlignment.Right);
lv.Visible = true ;

OleDbDataReader reader ;
string strCommand = "SELECT * FROM myReport" ;
this.conConnection.Open ( ) OleDbCommand cmd = new OleDbCommand ( strCommand , conConnection ) ;
reader = cmd.ExecuteReader ( ) ;

while ( reader.Read ( ) )
{
ListViewItem li = new ListViewItem ( ) ;
li.SubItems.Clear ( ) ;
li.SubItems[0].Text = reader["Price"].ToString();
li.SubItems.Add(reader["Date"].ToString());
li.SubItems.Add(reader["High"].ToString());
li.SubItems.Add(reader["Low"].ToString());
li.SubItems.Add(reader["High"-"Low"].ToString());
lv.Items.Add ( li ) ;
}
********************************************
Posted

You can do it in your CommandText i.e

SQL
SELECT Price, Date, High, Low, (High - Low) AS Difference From myReport


then you can read the rows from your reader like this

li.SubItems.Add(reader["Price"].ToString();
li.SubItems.Add(reader["Date"].ToString();
li.SubItems.Add(reader["High"].ToString();
li.SubItems.Add(reader["Low"].ToString();
li.SubItems.Add(reader["Difference"].ToString();


although I think this is the wrong way to go about things. If I were you I would not store my data in the database as strings, instead I would use the appropriate data type and retrieve the data like this

li.SubItems.Add(reader.GetDecimal(0);
li.SubItems.Add(reader.GetDate(1); etc


This is obviously after you have checked that the reader is not returning any Null values.

Hope this helps
 
Share this answer
 
You have to get the two values and subtract those.

For example, if your values are of type int:

C#
int high = ( int ) reader[ "High" ];
int low  = ( int ) reader[ "Low" ];
int range = high - low;

li.SubItems.Add( range.ToString() );


Nick
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900