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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:12:32+00:00 2026-05-26T08:12:32+00:00

I have an ‘ObservableCollection’ that stores formatted ColumnLabels that need pasting into DataGrid. The

  • 0

I have an ‘ObservableCollection’ that stores formatted ColumnLabels that need pasting into DataGrid.

The collection could have numerous different entries and can be of size ‘N’ therefore there will be ‘N’ number of columns in my DataGrid. Defined below:

    private ObservableCollection<string> _stockColumnLabels;
    public ObservableCollection<string> StockColumnLabels 
    {
        get 
        {
            if (_stockColumnLabels == null)
            {
                _stockColumnLabels = new ObservableCollection<string>();
                return _stockColumnLabels;
            }
            return _stockColumnLabels; 
        }
        set
        {
            _stockColumnLabels = value;
            //OnPropertyChanged("StockColumnLabels");
        } 
    }

Sample data this collection will hold e.g:

  1. “1”
  2. “2”
  3. “3”
  4. “4”
  5. “5”

N.B, all strings of course, essentially can be seen as a single row of data that represent columns when viewed on the GUI.

The collection is wrapped into a property for the sake of DataBinding and lives in the ViewModel and the DataGrid lives in the View. The DataContext of the View is set at RunTime when the App.cs file is run !

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);

        MainWindow window = new MainWindow();
        window.DataContext = new FSEnquiryViewModel();
        window.Show();
    }
}

GUI (VIEW)

At present due to the nature of the requirements being set on this project, the Column names have to be formatted out of ProductCode so to make life easier, I created one DataGrid that simply holds a single row of data which are column names and then another DataGrid just below that will hold row values. Don’t worry about the second DataGrid, I will sort that later, as the set-up is exactly the same. For now can someone help me ‘Bind’ my ObservableCollection found in my ViewModel to my DataGrid, it’s just a single strip of Data.

To Look Like

           1         2         ...       n         <- 'DataGrid1'
Row1 |     1    |    3    |    5    |    6    |    <- 'DataGrid2'
Row2 |     3    |    2    |    1    |    8    |
  • 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-05-26T08:12:32+00:00Added an answer on May 26, 2026 at 8:12 am

    My code below hopes to answer the question that allows you to bind data from an unknown number of sources, each results could be unknown in size as well as the potential number of columns that could be used. Then shape the results slowly row by row into a dynamic collection which represents the final data layout.

    Basically more or less the same as BlindMeis answer but he posted code that was syntactially incorrect and didn’t look like it had been tested before posting. Mine below is however code that I am currently using.

    Below you will find what worked for me assuming you have a result set or sets to begin with.

    1. Declare a table to hold your results.

      DataTable shapedResultsTable = new DataTable();

    2. Next thing, would be to filter some results from a DB call or procedure who results you are trying to re-shape. Something like:

          foreach (DataRow row in whFreeResults.Rows)
          {
              // filter your results and strip exactly the data you need to represent your column names.
              // dump your results into a sample list e.g: 'columnNamesFormatted'
      
          }
      
    3. Once you have a list of column names, add them to the your data table declare above.

          foreach (string column in columnNamesFormatted)
          {
              shapedResultsTable.Columns.Add(column, typeof(string));
          }
      
    4. Next, declare, instantiate and populate a list of unknown size because your results are of size n or amount n, to store your row values.

    5. Next, repeat step 2 and 3 for every row of data you would like to show on your new Shaped Results Data Table, more over one for each row found in your result set.

    6. Once you have a few collections that represent each row you want to add to your Data Table. Add them in the order you would want them to be displayed in.

      shapedResultsTable.Rows.Add(totalsArray.ToArray());
      shapedResultsTable.Rows.Add(branchRowValues.ToArray());

    7. Finally, assign your DataTable to a ‘DataBinded Property’.

      StockResultsTable = shapedResultsTable;

    8. The property that is bound could look like:

      private DataTable _stockResultsTable;
      public DataTable StockResultsTable
      {
          get { return _stockResultsTable; }
          set 
          { 
              _stockResultsTable = value;
              OnPropertyChanged("StockResultsTable"); // <--- defo' need this one.
          }
      }
      
    9. Finally the xaml on the front end could look like:

              <DataGrid Height="179" 
                HorizontalAlignment="Left" 
                Margin="151,0,0,211" 
                Name="dgStockInfo" 
                VerticalAlignment="Bottom" 
                Width="393" 
                ItemsSource="{Binding Path = StockResultsTable}" AutoGenerateColumns="True" Padding="0,10" FontSize="14" />
      

    I don’t know why the auto generate columns property is turned on but this works for me.

    Steps 1-9 worked for me, working with a sample data set of results returned from a query or SProc, the steps above briefly outline what I did in displaying the results from the numerous queries I had to run to get very different data from many different SProcs but had a common underlying theme which allowed them to be displayed under common column names.

    Be mindful of the fact that you will run into trouble where adding a row of data which has more members in it that the DataTable has columns. A DT which has 6 columns but you are adding a row which has come from a different Query altogether, which has 7 individual cells of data. This will not work, simply add in the new column that is not already there and try again.

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

Sidebar

Related Questions

Have a build process that can't be edited and need to pack another war
Have a flash player that pops out into a separate popup browser window. And
Have you ever seen any of there error messages? -- SQL Server 2000 Could
Have noticed issue while testing iphone app that if one quickly opens/dismisses a modal
have a table that dynamically generates text boxes in run time. I want to
I have a project that adds elements to an AutoCad drawing. I noticed that
I have a script that appends some rows to a table. One of the
I have a new web app that is packaged as a WAR as part
i have a input tag which is non editable, but some times i need
Have following Java code,that creates StringBuilder with \n,i.e. carriage return delimiters: while (scanner.hasNextLine()){ sb.append(scanner.nextLine()).append(\n);

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.