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

I can't seem to figure out exactly what I'm doing wrong here.
When I run the code my List isn't being displayed in my DataGridView.

Can some one please help me out here?

Thanks in advance.
C#
class Stock : INotifyPropertyChanged
{
  string sym;
  float  prc;
  int    sz;

  public event PropertyChangedEventHandler PropertyChanged;
  
  public Stock(string sym, float prc, int sz)
  {
    this.sym = sym;
    this.prc = prc;
    this.sz  = sz;
  }

  public string Symbol
  {
     get{return sym;}
  }
  
  public float Price
  {
    get{return prc;}
    set
    {
       if (prc != value)
       {
         price = value;
         OnPropertyChanged("Price");
    }
  }
  
  public int Size
  {
    get{return sz;}
    set 
    {
       if (sz != value)
       {
          sz = value;
          OnPropertyChanged("Size");
       }
    }
  }

  private void OnPropertyChanged(string propertyName)
  {
     if (PropertyChanged != null)
     {
         PropertyChanged(propertyName);
     }
  }
}

class StocksManager
{
   // Stock Market Data API Feed
   MktDataAPI  mktData;
   Thread t;
   Queue<Stock> q;
   EventWaitHandle ewh;
   object sync;
   Dictionary<string, Stock> stocksCollec;
   List<Stock> tempStocksCollec;

  public StocksManager
  {
     mktData = new MktDataAPI();
     mktData.UpdateMktData += new UpdateMktDataEventHndlr(mktData_UpdateMktData);
     q = new Queue<Stock>();
     sync = new object();
     stocksCollec = new Dictionary<string, Stock>();
     tempStocksCollec = new Dictionary<string, Stock>();
     t = new Thread(worker);

     t.IsBackGround = true;
     t.Start();
  }

  public List<Stock> StocksCollection
  {
     get{return tempStocksCollec;}
  }
  
  private void mktData_UpdateMktData(string sym, float prc, int sz)
  {
     lock(sync)
     {
        q.Enqueue(new Stock(sym, prc, sz));
     }
  }

  private void worker()
  {
     while(true)
     {
        Stock s = null;
        
        lock(sync)
        {
           if (q.Count > 0)
           { 
               s = q.Dequeue();
           }
        }
 
        if (s != null)
        {
           string sym = s.Symbol;
           float  prc = s.Price;
           int    sz  = s.Size;
   
           if(!stocksCollec.ContainsKey(sym))
           {
              stocksCollec.Add(new Stock(sym,prc,sz));
           }
           else
           {
              stocksCollec[sym].Price = prc;
              stocksCollec[sym].Size  = sz;
           }
   
           tempStocksCollec.AddRange(stocksCollec.Values);
        }
        else
        {
           signal.WaitOne();
        }
     }
  }
     
}

// GUI
StocksManager stkMngr;
public Form1()
{
   InitializeComponent();
   
   stkMngr = new StockManager();
   
   dataGridView1.DataSource = stkMngr.StocksCollection;
}
Posted
Updated 19-Feb-12 8:10am
v2
Comments
André Kraak 19-Feb-12 14:10pm    
Edited question:
Added pre tags

I'm really not sure where you got that collection of bits and pieces from, but I think you should put it back there. That won't even compile.

C#
List<Stock> tempStocksCollec;
...
    tempStocksCollec = new Dictionary<string, Stock>();
...
  public List<Stock> StocksCollection
  {
     get{return tempStocksCollec;}
  }
...
   stkMngr = new StockManager();
   dataGridView1.DataSource = stkMngr.StocksCollection;
You can't assign a Dictionary to a List - they are completely different types!
And I have never tried to use a Dictionary as a DataSource - I'm not sure what would happen, but I don't think it would work quite as well as your seem to think...
 
Share this answer
 
Comments
d.allen101 19-Feb-12 14:41pm    
I'm sorry it was a typo it should have been: tempStocksCollec.AddRange(stocksCollec.Values);
OriginalGriff 19-Feb-12 14:43pm    
That still isn't going to get you a clean compilation.
And how did you introduce typos with cut and paste from your code?
d.allen101 19-Feb-12 14:53pm    
i didn't cut and paste from my code I was trying to give an example of what i'm doing. here's the cut and paste:

public class Stock : INotifyPropertyChanged
{
private string symbol;
private double price;
private int size;

public event PropertyChangedEventHandler PropertyChanged;

public Stock(string symbol, double price, int size)
{
this.symbol = symbol;
this.price = price;
this.size = size;
}

public string Symbol
{
get { return symbol; }
}

public double Price
{
get { return price; }
set
{
if (price != value)
{
price = value;
OnPropertyChanged("Price");
}
}
}

public int Size
{
get { return size; }
set
{
if (size != value)
{
size = value;
OnPropertyChanged("Size");
}
}
}

private void OnPropertyChanged(string propertyName)
{
if(PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}

public class StocksManager
{
// Members
int NUM_PROCESSORS = Environment.ProcessorCount;

MktDataSim mktData;
List<Thread> workers;
EventWaitHandle signal;
Queue<stock> workQueue;
object sync;
Dictionary<string, stock=""> stocksCollec;
List<stock> tempStocksCollec;

// Constructor
public StocksManager()
{
mktData = new MktDataSim();
workers = new List<Thread>();
signal = new AutoResetEvent(false);
workQueue = new Queue<stock>();
sync = new object();
stocksCollec = new Dictionary<string, stock="">();
tempStocksCollec = new List<stock>();

mktData.StockUpdateHandler += new StockUpdateEventHandler
(mktData_StockUpdateHandler);

for (int i = 0; i < NUM_PROCESSORS; i++)
{
workers.Add(new Thread(worker));
workers[i].IsBackground = true;
workers[i].Start();
}
}

// Properties
public List<stock> StockCollection
{
get { return tempStocksCollec; }
}

// Methods/Event Handlers
private void mktData_StockUpdateHandler(string symbol, double price, int size)
{
getMktData(new Stock(symbol, price, size));
}


private void getMktData(Stock stock)
{
lock (sync)
workQueue.Enqueue(stock);
signal.Set();
}

private void worker()
{
while (true)
{
Stock stock = null;

lock (sync)
{
if (workQueue.Count > 0)
stock = workQueue.Dequeue();
}

if (stock != null)
{
string sym = stock.Symbol;
double prc = stock.Price;
int sz = stock.Size;

tempStocksCollec.Clear();

if (!stocksCollec.ContainsKey(sym))
{
stocksCollec.Add(sym, new Stock(sym, prc, sz));
}
else
{
stocksCollec[sym].Price = prc;
stocksCollec[sym].Size = sz;
}

tempStocksCollec.AddRange(stocksCol
Your code look very much flawed or you have not put here correctly. I took your second code which was there in comment did formatting as it lost formatting when i copied in visual Studio.

I noticed following points

1. stocksCollec = new Dictionary<string,>(); another type parameter missing.
Replaced with
stocksCollec = new Dictionary<string,stock>();

2. Shouldn't mktData.StockUpdateHandler += new StockUpdateEventHandler (mktData_StockUpdateHandler);
be the other way round.
I mean StockUpdateHandler should be Delegate name and StockUpdateEventHandler should be Event name.

but then its just naming convention issue.

3. Queue workQueue and List tempStocksCollec has type parameter missing
Updated to Queue<Stock> workQueue;
List<Stock> tempStocksCollec;

4. stocksCollec is Dictionary and tempStocksCollec is List. you can't add stackCollecs to tempStocksCollecs
unless you take only key or value from the dictionary. like what i did

tempStocksCollec.AddRange(stocksCollec.Values.ToList());

5. You are initializing MktDataSim object in the constructor of StocksManager and listening to event fired. Who is then firing
the event. I don't know about your requirement but looks like you should have MktDataSim as parameter in the Stock Manager constructor
Someone can correct me if i m wrong on this.

Anyway i modified code and now I can see
Symbol,Price and Size columns in the grid.

This is the code after modification.Hope it will help you bit or atleast will point you in right direction

C#
public partial class Form1 : Form
{
   public Form1()
   {
      InitializeComponent();
      var stkMngr = new StocksManager();
      dataGridView1.DataSource = stkMngr.StockCollection;;
   }
}
     
public class StocksManager 
{ 
   int NUM_PROCESSORS = Environment.ProcessorCount;
   MktDataSim mktData; 
   List<thread> workers;
   EventWaitHandle signal; 
   Queue<stock> workQueue;
   object sync;
   Dictionary<string,stock> stocksCollec; 
   List<stock> tempStocksCollec; 
      
   public StocksManager()
   { 
      mktData = new MktDataSim();
      workers = new List<thread>(); 
      signal = new AutoResetEvent(false); 
      workQueue = new Queue<stock>(); 
      sync = new object(); 
      stocksCollec = new Dictionary<string,stock>();
      tempStocksCollec = new List<stock>();
      mktData.StockUpdateHandler += new StockUpdateEventHandler (mktData_StockUpdateHandler); 
      
      for (int i = 0; i < NUM_PROCESSORS; i++)
      { 
         workers.Add(new Thread(worker)); 
         workers[i].IsBackground = true; 
         workers[i].Start(); 
      }
   }
   
   public List<stock> StockCollection 
   {
      get { return tempStocksCollec; }
   }
   
   private void mktData_StockUpdateHandler(string symbol, double price, int size)
   { 
      getMktData(new Stock(symbol, price, size));
   } 
   
   private void getMktData(Stock stock)
   {
      lock (sync) workQueue.Enqueue(stock); 
      signal.Set(); 
   } 
   private void worker() 
   {
      while (true)
      {
         Stock stock = null; 
         lock (sync) 
         {
            if (workQueue.Count > 0)
               stock = workQueue.Dequeue(); 
                        } 
            if (stock != null)
            { 
               string sym = stock.Symbol;
               double prc = stock.Price;
               int sz = stock.Size; 
               tempStocksCollec.Clear(); 
               
               if (!stocksCollec.ContainsKey(sym)) 
                  stocksCollec.Add(sym, new Stock(sym, prc, sz));
               else
                  stocksCollec[sym].Price = prc; stocksCollec[sym].Size = sz; 

               tempStocksCollec.AddRange(stocksCollec.Values.ToList());
            }
         }
      }
   }

   public delegate void StockUpdateEventHandler(string symbol, double price, int size);
   public class MktDataSim
   {
      public event StockUpdateEventHandler StockUpdateHandler;
   }

   public class Stock : INotifyPropertyChanged
   { 
      private string symbol; 
      private double price;
      private int size;
      public event PropertyChangedEventHandler PropertyChanged; 
      
      public Stock(string symbol, double price, int size) 
      { 
         this.symbol = symbol; this.price = price; this.size = size; 
      } 
      
      public string Symbol
      { 
         get { return symbol; }
      } 
      
      public double Price 
      {
         get { return price; } 
         set 
         { 
            if (price != value) 
            { 
               price = value;
               OnPropertyChanged("Price");
            } 
         } 
      } 

      public int Size 
      {
         get { return size; } 
         set 
         {
            if (size != value)
            {
               size = value; 
               OnPropertyChanged("Size"); 
            }
         }
      }
      
      private void OnPropertyChanged(string propertyName) 
      { 
         if(PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
      } 
   }
}
 
Share this answer
 
Comments
fjdiewornncalwe 20-Feb-12 14:34pm    
Just added pre tags to the source and cleaned up the formatting a bit.

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