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. C#
  4. Using .NET DataGrid with Collections

Using .NET DataGrid with Collections

Scheduled Pinned Locked Moved C#
csharpwpfwcf
2 Posts 2 Posters 0 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.
  • T Offline
    T Offline
    TedWeston
    wrote on last edited by
    #1

    I want to use the Windows .NET DataGrid to display items in a collection. This is quite easy dataGrid1.DataSource = myCollection; BUT the columns appear in an arbitary order (not as declared in the item class). Is there anyway to specify the order that public properties of a class are read by the data binding mechanism of the datagrid. I know that gridcolumnstyles can be used but only where tables are used. Any clues would be gratefully appreciated.

    T 1 Reply Last reply
    0
    • T TedWeston

      I want to use the Windows .NET DataGrid to display items in a collection. This is quite easy dataGrid1.DataSource = myCollection; BUT the columns appear in an arbitary order (not as declared in the item class). Is there anyway to specify the order that public properties of a class are read by the data binding mechanism of the datagrid. I know that gridcolumnstyles can be used but only where tables are used. Any clues would be gratefully appreciated.

      T Offline
      T Offline
      the last free name
      wrote on last edited by
      #2

      I've used this approach many times DataGridTableStyle gridStyle = new DataGridTableStyle(); gridStyle.ReadOnly = true; gridStyle.MappingName = myCollection.GetType().Name; DataGridTextBoxColumn cs = new DataGridTextBoxColumn(); cs.MappingName = "VehicleID"; // public property name cs.HeaderText = "Vehicle ID"; // column header cs.Alignment = HorizontalAlignment.Center; cs.Width = 80; cs.TextBox.Enabled = false; gridStyle.GridColumnStyles.Add(cs); // repeat for other properties // add style to collection dataGrid1.TableStyles.Add(gridStyle); the column order will follow the order in which the DataGridTextboxColumns are added to the gridstyle.

      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