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 8587865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:40:23+00:00 2026-06-11T22:40:23+00:00

I have an object called TestData that contains two properties, a string and an

  • 0

I have an object called TestData that contains two properties, a string and an ObservableCollection. I have an ObservableCollection that I use to generate columns in a DataGrid in code. I have no problem dynamically generating the DataGridTextColumn and their respective headers based off of the TestData object, but I cannot bind the DataGridTextColumn Binding property to the ObservableCollection inside of TestData.

Now that I have stated the problem in words, let me clear it up by putting up the code:

TestData.cs

public class TestData
{
    private ObservableCollection<string> data = new ObservableCollection<string>();
    public string Header { get; set; }
    public ObservableCollection<string> Data
    {
        get
        {
            return data;
        }

        set
        {
            data = value;
        }
    }
}

MainWindow.xaml.cs Code Behind

public partial class MainWindow : Window
{
    private ViewModel viewModel = new ViewModel();
    private List<string> headers = new List<string>();
    private ObservableCollection<TestData> mainData = new ObservableCollection<TestData>();        


    public MainWindow(ViewModel vm)
    {
        InitializeComponent();
        viewModel = vm;
        this.DataContext = viewModel;     


        mainData = viewModel.MainData;

        this.spreadSheet.ItemsSource = viewModel.MainData;            

        foreach (TestData data in mainData)      
        {
            if (headers.Contains(data.Header) == false)
            {
                headers.Add(data.Header);
                DataGridTextColumn col = new DataGridTextColumn();                    
                int x = viewModel.MainData.IndexOf(data);                    
                Binding bind = new Binding("Data");                    
                bind.BindsDirectlyToSource = true;                    
                bind.Source = mainData[x];                    
                col.Binding = bind;
                col.Header = data.Header;                                      
                this.spreadSheet.Columns.Add(col);        
            }

        }
    }
}

XAML MainWindow.xaml

<Grid>                
    <DataGrid   AutoGenerateColumns="False" Height="Auto" HorizontalAlignment="Stretch"  Name="spreadSheet" VerticalAlignment="Stretch"  Width="Auto" />

</Grid>

If I place 4 TestData objects in my ObservableCollection I get their headers fine, but all of the data in the ObservableCollection inside the TestData objects just show as (Collection) in the DataGrid.

Thanks in advance!

  • 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-11T22:40:25+00:00Added an answer on June 11, 2026 at 10:40 pm

    I don’t think you are looking at this correctly.
    You don’t bind a separate collection with a single string to each column.
    You bind a single two dimensional collection of rows and columns to the DataGrid as a whole.

    Notice a column does not have an ItemsSource Property. Only a path Property.
    DataGridTextColumn

    DataGrid has an ItemsSource Property. DataGrid

    public class Myrow 
    {
        public string Col1 { get; private set; }  
        public string Col2 { get; private set; } 
        public Myrow (string col1, string col2) { Col1 = col1, Col2 = Col2 }
    }
    
    ObservableCollection<Myrow> Myrows ...
    

    MyRows is bound to the DataGrid (not column)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object called exampleObject with two string properties: moduleName , and pool
I have an object called FormObject that contains two ArrayLists - oldBooks and newBooks
I have an object called users that I use to store user objects that
I have an object called Person that has the following properties: int id; Name
I have an object that has properties of another object and one called DataValue
I have an object called brain brain has 8 properties that contain an Animal
I have an object called particle that has its own properties (position, velocity, etc.)
I have an object called Layer that has some attributes and some methodes. i
I have an object called Document that has a one-to-many relationship with DocumentPersonCc. I
Here is the scenario: I have an object called a Transaction that needs to

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.