Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Dynamic WPF DataGrid

Dynamic WPF DataGrid

Scheduled Pinned Locked Moved WPF
wpfhelpcsharpwcfperformance
6 Posts 2 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    I need to automatically generate a WPF DataGrid at runtime. Some of the columns will be known, and some will be added based off user selections. In all cases, I'll know the data types. The column definitions will all be rows in some table, allowing me to create any number of columns. The problem that I'm going to run into is binding. Since there will be an unknown number of columns, I can't create an "concrete" entity to bind to. So, the real question here is how to represent the data when I don't know all the columns. I came across some examples of ExpandoObject usage and put together this sample:

    static void Main(string[] args)
    {
    // Assigning properties and their values at runtime
    var x = new ExpandoObject() as IDictionary;
    x.Add("NewProp", "Hello World!");

    IDictionary propertyValues = x;
    foreach (var property in propertyValues.Keys)
    {
        Console.WriteLine(String.Format("{0} : {1}", property, propertyValues\[property\]));
    }
    
    
    // Creating properties in code and assigning data at runtime
    dynamic expando = new ExpandoObject();
    expando.SomeStringProp = "C";
    expando.SomeNumberProp = 12;
            
    expando.MyMethod = new Func(() =>
    {
        return 55;
    });
    
    Console.WriteLine(expando.SomeStringProp);
    Console.WriteLine(expando.SomeNumberProp);
    Console.WriteLine(expando.MyMethod());
    Console.ReadLine();
    

    }

    I'm concerned about performance here. If anyone has a better way I'd like to hear about it. Thanks!

    If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

    Richard DeemingR 1 Reply Last reply
    0
    • K Kevin Marois

      I need to automatically generate a WPF DataGrid at runtime. Some of the columns will be known, and some will be added based off user selections. In all cases, I'll know the data types. The column definitions will all be rows in some table, allowing me to create any number of columns. The problem that I'm going to run into is binding. Since there will be an unknown number of columns, I can't create an "concrete" entity to bind to. So, the real question here is how to represent the data when I don't know all the columns. I came across some examples of ExpandoObject usage and put together this sample:

      static void Main(string[] args)
      {
      // Assigning properties and their values at runtime
      var x = new ExpandoObject() as IDictionary;
      x.Add("NewProp", "Hello World!");

      IDictionary propertyValues = x;
      foreach (var property in propertyValues.Keys)
      {
          Console.WriteLine(String.Format("{0} : {1}", property, propertyValues\[property\]));
      }
      
      
      // Creating properties in code and assigning data at runtime
      dynamic expando = new ExpandoObject();
      expando.SomeStringProp = "C";
      expando.SomeNumberProp = 12;
              
      expando.MyMethod = new Func(() =>
      {
          return 55;
      });
      
      Console.WriteLine(expando.SomeStringProp);
      Console.WriteLine(expando.SomeNumberProp);
      Console.WriteLine(expando.MyMethod());
      Console.ReadLine();
      

      }

      I'm concerned about performance here. If anyone has a better way I'd like to hear about it. Thanks!

      If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      How about loading the data into a DataTable and binding the grid to the table's .DefaultView property?


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      K 2 Replies Last reply
      0
      • Richard DeemingR Richard Deeming

        How about loading the data into a DataTable and binding the grid to the table's .DefaultView property?


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        K Offline
        K Offline
        Kevin Marois
        wrote on last edited by
        #3

        Ya, a table would be easier to build. I'll look into it. Thanks!

        If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

        1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          How about loading the data into a DataTable and binding the grid to the table's .DefaultView property?


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          K Offline
          K Offline
          Kevin Marois
          wrote on last edited by
          #4

          Ok, I'm stuck here. I can see rows - no data just the highlight - and no columns. What am i doing wrong?? XAML

          ViewModel

          private DataTable _BidBudgetData;
          public DataTable BidBudgetData
          {
          get { return _BidBudgetData; }
          set
          {
          if (_BidBudgetData != value)
          {
          _BidBudgetData = value;
          RaisePropertyChanged("BidBudgetData");
          }
          }
          }

          private void CreateBidBudgetTable()
          {
          var bidBudgetData = new DataTable();
          bidBudgetData.Columns.Add("Plan Type", typeof(string));
          bidBudgetData.Columns.Add("Living Sq Ft", typeof(decimal));

          foreach (var planType in ProjectPlanTypeSummaries.OrderBy(x =>x.Plan).ThenBy(x => x.Elevation))
          {
              DataRow row = bidBudgetData.NewRow();
              row\["Plan Type"\] = $"{planType.Plan}{planType.Elevation}";
              row\["Living Sq Ft"\] = planType.LivingSqFt;
              bidBudgetData.Rows.Add(row);
          }
          
          BidBudgetData = bidBudgetData;
          RaisePropertyChanged("BidBudgetData");
          

          }

          If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

          Richard DeemingR 1 Reply Last reply
          0
          • K Kevin Marois

            Ok, I'm stuck here. I can see rows - no data just the highlight - and no columns. What am i doing wrong?? XAML

            ViewModel

            private DataTable _BidBudgetData;
            public DataTable BidBudgetData
            {
            get { return _BidBudgetData; }
            set
            {
            if (_BidBudgetData != value)
            {
            _BidBudgetData = value;
            RaisePropertyChanged("BidBudgetData");
            }
            }
            }

            private void CreateBidBudgetTable()
            {
            var bidBudgetData = new DataTable();
            bidBudgetData.Columns.Add("Plan Type", typeof(string));
            bidBudgetData.Columns.Add("Living Sq Ft", typeof(decimal));

            foreach (var planType in ProjectPlanTypeSummaries.OrderBy(x =>x.Plan).ThenBy(x => x.Elevation))
            {
                DataRow row = bidBudgetData.NewRow();
                row\["Plan Type"\] = $"{planType.Plan}{planType.Elevation}";
                row\["Living Sq Ft"\] = planType.LivingSqFt;
                bidBudgetData.Rows.Add(row);
            }
            
            BidBudgetData = bidBudgetData;
            RaisePropertyChanged("BidBudgetData");
            

            }

            If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #5

            You've set AutoGenerateColumns to false, but you haven't manually defined any columns in the DataGrid.


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            K 1 Reply Last reply
            0
            • Richard DeemingR Richard Deeming

              You've set AutoGenerateColumns to false, but you haven't manually defined any columns in the DataGrid.


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              K Offline
              K Offline
              Kevin Marois
              wrote on last edited by
              #6

              Yup. I'm an idiot. I spent hours trying to figure out why I'm not seeing anyting. Thanks!

              If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups