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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:41:13+00:00 2026-05-22T02:41:13+00:00

I need to bind some data to a DataGrid with variable number of columns.

  • 0

I need to bind some data to a DataGrid with variable number of columns. I made it work using following code:

int n = 0;
foreach (string title in TitleList)
{
    DataGridTextColumn col = new DataGridTextColumn();
    col.Header = title;
    Binding binding = new Binding(string.Format("DataList[{0}]", n++));
    binding.Mode = BindingMode.TwoWay;
    col.Binding = binding;
    grid.Columns.Add(col);
}

where DataList is declared as:

public ObservableCollection<double> DataList { get; set; }

and TitleList is declared as:

public ObservableCollection<string> TitleList { get; set; }

The problem is that, even though I specified TwoWay binding, it is really one-way. When I click a cell to try to edit, I got an exception “‘EditItem’ is not allowed for this view”. Did I just miss something in the binding expression?

P.S. I found an article from Deborah “Populating a DataGrid with Dynamic Columns in a Silverlight Application using MVVM”. However, I had hard time to make it work for my case (specifically, I can’t make the header binding work). Even if it worked, I’m still facing issues like inconsistent cell styles. That’s why I’m wondering if I could make my above code work – with a little tweak?

EDIT: I found another post which might be related to my problem: Implicit Two Way binding. It looks if you bind to a list of string to a TextBox using

<TextBox Text="{Binding}"/>

You will get an error like “Two-way binding requires Path or XPath”. But the problem can easily be fixed by using

<TextBox Text="{Binding Path=DataContext, RelativeSource={RelativeSource Self}}"/>

or

<TextBox Text="{Binding .}"/>

Can anybody give me a hint if my problem can be solved in a similar way?

  • 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-22T02:41:14+00:00Added an answer on May 22, 2026 at 2:41 am

    Do you bind to an indexer?. can you show us how your DataList Property looks like?

    i did the same a while ago with an indexed property.

     public SomeObjectWithIndexer DataList
     {get; set;}
    
    
     public class SomeObjectWithIndexer 
     {
          public string this
          {
              get { ... }
              set { ... }//<-- you need this one for TwoWay
          }
     }
    

    EDIT: the reason that you cant edit your Property, is that you try to edit a “double field”.
    one workaround would be to wrap your double into a class with INotifyPropertyChanged.

    public class DataListItem
    {
        public double MyValue { get; set;}//with OnPropertyChanged() and stuff
    }
    

    then you can use a

    ObservableCollection<DataListItem>
    

    and you can edit your value. the question wether the index are always the same stay still around 🙂

    Binding binding = new Binding(string.Format("DataList[{0}].MyValue", n++));
    

    EDIT2: working example: just to show twoway is working

    public class DataItem
    {
        public string Name { get; set; }
        public ObservableCollection<DataListItem> DataList { get; set; }
    
        public DataItem()
        {
            this.DataList = new ObservableCollection<DataListItem>();
        }
    }
    

    Wrapper for double:

    public class DataListItem
    {
        private double myValue;
        public double MyValue
        {
            get { return myValue; }
            set { myValue = value; }//<-- set breakpoint here to see that edit is working
        }
    }
    

    usercontrol with a datagrid

    <UserControl x:Class="WpfStackoverflow.IndexCollectionDataGrid"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <DataGrid ItemsSource="{Binding MyList}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Name" Binding="{Binding Path=Name}" />
            <DataGridTextColumn Header="Index1" Binding="{Binding Path=DataList[0].MyValue, Mode=TwoWay}" />
            <DataGridTextColumn Header="Index2" Binding="{Binding Path=DataList[1].MyValue, Mode=TwoWay}" />
        </DataGrid.Columns>
    </DataGrid>
    </UserControl>
    

    .cs

    public partial class IndexCollectionDataGrid : UserControl
    {
        public IndexCollectionDataGrid()
        {
            InitializeComponent();
            this.MyList = new ObservableCollection<DataItem>();
    
            var m1 = new DataItem() {Name = "test1"};
            m1.DataList.Add(new DataListItem() { MyValue = 10 });
            m1.DataList.Add(new DataListItem() { MyValue = 20 });
    
            var m2 = new DataItem() { Name = "test2" };
            m2.DataList.Add(new DataListItem() { MyValue = 100 });
            m2.DataList.Add(new DataListItem() { MyValue = 200 });
    
            this.MyList.Add(m1);
            this.MyList.Add(m2);
    
            this.DataContext = this;
        }
    
        public ObservableCollection<DataItem> MyList { get; set; }
    }
    

    i hope you get in the right direction with this example.

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

Sidebar

Related Questions

I use the following code to bind an arraylist to a datagrid //fill datagrid
I need to bind the column Header text of a DataGrid to a Resource
I am using Flot to chart some data that I pull back from the
I need to bind labels or items in a toolstrip to variables in Design
I need to bind a method into a function-callback, except this snippet is not
I need a way to bind POJO objects to an external entity, that could
Need to locate the following pattern: The letter I followed by a space then
need ask you about some help. I have web app running in Net 2.0.
Im using a List<Patient> object as the data source to a data grid view.
I'm trying to do some simple data retrieval with C# and Directory Services, but

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.