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

  • SEARCH
  • Home
  • 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 9038017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:23:05+00:00 2026-06-16T09:23:05+00:00

I am quite new to WPF (from Winforms). I am using .Net 4.5 and

  • 0

I am quite new to WPF (from Winforms). I am using .Net 4.5 and the default DataGrid that comes along with the framework in WPF. The columns are created dynamically because I do not know at compile time. Now, based on data some columns will be read-only and some will be of ComboBox type.

  • How can I apply this logic dynamically while creating the columns dynamically as shown below. here is the code which I wrote so far. Whenever the data changes, the columns are generated dynamically based on the data.
  • Also, how do I generate "different types" of column dynamically (ComboBox, TextBox, etc…) based on data. The MVVM-ish way in WPF is kind of restricting me because I do not have much knowledge about templating. I am sure it should be easy once I get through.

NB: Currently all this is working fine. I have a read-only databound grid. But, there is no support for selective editable columns and selective ComboBox columns.

public class DatagridExtension {
    
    public static readonly DependencyProperty RefDataSourceProperty =
        DependencyProperty.RegisterAttached(
            "RefDataSource",
            typeof(RefDataRecord),
            typeof(DatagridExtension),
            new PropertyMetadata( default(RefDataRecord), OnRefDataSourceChanged)
        );


    private static void OnRefDataSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var grid = d as DataGrid;
        var dataSource = e.NewValue as RefDataRecord;

        grid.ItemsSource = dataSource;
        grid.Columns.Clear();
        int count = 0;
        foreach (var col in dataSource.Columns)
        {
            grid.Columns.Add(
                new DataGridTextColumn
                    {
                        Header = col.Name,
                        Binding = new Binding(string.Format("[{0}]", count))
                    }
                );
            count++;
        }
    }

    public static RefDataRecord GetRefDataSource(DependencyObject dependencyObject)
    {
        return (RefDataRecord) dependencyObject.GetValue(RefDataSourceProperty);
    }

    public static void SetRefDataSource(DependencyObject dependencyObject, RefDataRecord value)
    {
        dependencyObject.SetValue(RefDataSourceProperty, value);
    }
}

http://msdn.microsoft.com/en-us/library/system.windows.controls.datagridtemplatecolumn.celltemplate(v=vs.95).aspx

  • 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-16T09:23:07+00:00Added an answer on June 16, 2026 at 9:23 am

    This is the correct answer – http://www.paulstovell.com/dynamic-datagrid (see the template creation logic dynamically. Its clever).

    And, MMVM will be achieved like this – http://www.codeproject.com/Articles/36462/Binding-a-ListView-to-a-Data-Matrix (almost what I have posted in the question)

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

Sidebar

Related Questions

I am quite new to WPF and XAML but my background is of ASP.NET
I have some .NET 4 entity framework objects that I get from the DB,
I am new to using styles, resources and templates in WPF. What I need
I must say I am quite new at WPF. I am building my first
Hi all i need help wih my combobox. Iam quite new in the WPF
I'm quite new to WPF and as new to Caliburn.Micro. What I'm trying to
I've just started learning WPF MVVM using Prism and Unity. Decoupling the view from
I understand that WPF coordinates are different from real screen coodinates (pixel coordinates) if
I am quite new to WPF and this might be a trivial question. What
I'm quite new to C# and .NET in general so this may seem like

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.