Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7891687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:40:53+00:00 2026-06-03T06:40:53+00:00

I’m trying to figure out how to bind the datasource of a DataGrid to

  • 0

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:
enter image description here

Thanks a lot – kcross!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T06:40:55+00:00Added an answer on June 3, 2026 at 6:40 am

    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 DataGridan IEnumerable(preferably an ObserrvableCollection) 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 Option classes. 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:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows;
    using System.Windows.Controls;
    using System.ComponentModel;
    using System.Collections.ObjectModel;
    namespace DataGridSpike
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            private List<Option> _unsortedOptions;
            private ObservableCollection<OptionRow> _groupedOptions;
    
            public ObservableCollection<OptionRow> GroupedOptions
            {
                get { return _groupedOptions; }
                set { _groupedOptions = value; }
            }
    
            public MainWindow()
            {
                var rnd=new Random();
                InitializeComponent();
    
                //Generate some random data
                _unsortedOptions=new List<Option>();
                for(int element=0;element<50;element++)
                {
                    double column=rnd.Next(-2,3);
                    int row=rnd.Next(0,9);
    
                    _unsortedOptions.Add(new Option { ColumnDefiningValue = column, RowDefiningValue = row });
                }
    
                //Prepare the data for the DataGrid
                //group and sort
                var rows = from option in _unsortedOptions
                           orderby option.ColumnDefiningValue
                           group option by option.RowDefiningValue into optionRow
                           orderby optionRow.Key ascending
                           select optionRow;
    
                //convert to ObservableCollection
                _groupedOptions = new ObservableCollection<OptionRow>();
                foreach (var groupedOptionRow in rows)
                {
                    var groupedRow = new OptionRow(groupedOptionRow);
                    _groupedOptions.Add(groupedRow);
                }
    
                //bind the ObservableCollection to the DataGrid
                DataContext = GroupedOptions;
            }
        }
    
        public class OptionRow
        {
            private List<Option> _options;
    
            public OptionRow(IEnumerable<Option> options)
            {
                _options = options.ToList();
            }
    
            public Option Minus2
            {
                get
                {
                    return (from option in _options
                           where option.ColumnDefiningValue == -2
                           select option).FirstOrDefault();
                }
            }
            public Option Minus1
            {
                get
                {
                    return (from option in _options
                            where option.ColumnDefiningValue == -1
                            select option).FirstOrDefault();
                }
            }
            public Option Zero
            {
                get
                {
                    return (from option in _options
                            where option.ColumnDefiningValue == 0
                            select option).FirstOrDefault();
                }
            }
            public Option Plus1
            {
                get
                {
                    return (from option in _options
                            where option.ColumnDefiningValue == 1
                            select option).FirstOrDefault();
                }
            }
            public Option Plus2
            {
                get
                {
                    return (from option in _options
                            where option.ColumnDefiningValue == 2
                            select option).FirstOrDefault();
                }
            }
        }
    
        public class Option:INotifyPropertyChanged
        {
    
            public override string ToString()
            {
                return string.Format("{0}-{1}", RowDefiningValue.ToString(),ColumnDefiningValue.ToString());
            }
    
            private double _columnDefiningValue;
            public double ColumnDefiningValue
            {
                get{return _columnDefiningValue;}
                set{_columnDefiningValue = value;
                    OnPropertyChanged("ColumndDefiningValue");
                }
            }
    
            private int _rowDefiningValue;
            public int RowDefiningValue
            {
                get{return _rowDefiningValue;}
                set{_rowDefiningValue = value;
                    OnPropertyChanged("RowDefiningValue");
                }
            }
    
            private void OnPropertyChanged(string propertyName)
            {
                if (PropertyChanged!=null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
        }
    }
    

    The XAML:

    <Window x:Class="DataGridSpike.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <DataGrid ItemsSource="{Binding}"/>
        </Grid>
    </Window>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.