I’m trying to figure out how to bind the datasource of a DataGrid to an ObservableCollection of ‘cells’. In particular, I have an ObservableCollection that holds instances of the following class:
public class Option : INotifyPropertyChanged
{
public Option()
{
}
// +-+- Static Information +-+-
public double spread = 0;
public double strike = 0;
public int daysToExpiry = 0;
public int put_call; // 0 = Call, 1 = Put
// Ticker References
public string fullTicker = "";
public string underlyingTicker = "";
//+-+-Properties used in Event Handlers+-+-//
private double price = 0;
public double Price
{
get { return price; }
set
{
price = value;
NotifyPropertyChanged("Price");
}
}
//+-+-+-+- Propoerty Changed Event & Hander +-+-+-+-+-//
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
On my DataGrid, I want to display these classes (I’m using TemplateColumns the Price and the ‘strike’ variables in each cell) such that they are grouped by “underlyingTicker” [which is a 4 character string] and by “spread” [which takes on 1 of 6 possible values defined in the background coding].
Currently, when I bind the DataGrid’s DataContext to the ObservableCollection, it shows each ‘Option’ as a row – and I can’t figure out how to specify what to group the pairs on…
This is what my datagrid looks like now:

Thanks a lot – kcross!
Like Dtex I do not entirely understand what you want to do. But I tried to make a simplification that hopefully will get you started.
You have to pass the
DataGridanIEnumerable(preferably anObserrvableCollection) of objects. The individual objects will translate to rows, the properties of these objects will translate to the column headers.So if you want the column headers to represent multiples of the standard deviation (right?) you will have to create an object that has these multiples as properties. The resulting cells will contain the
Optionclasses. To represent these you will have to define a DataTemplate or override the ToString() function. I think you did the former judging from your example.The code behind:
The XAML: