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. filling a column with property value of object

filling a column with property value of object

Scheduled Pinned Locked Moved C#
csstutorial
4 Posts 3 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.
  • A Offline
    A Offline
    Aresto1964
    wrote on last edited by
    #1

    I have two classes like this public myclass1 { public int Id {set;get;) public myclass2 MyClass2{set;get;) } public myclass2 { public int Id {set;get;} public string Name {set;get;} } public list mylist = new List() ; my list has been bonded to datagridview . when displying the collection in the grid , the myclass1 is displaying the object name "DomaninName.myclass2" how to display the propery "Name" of myclass2

    J OriginalGriffO 2 Replies Last reply
    0
    • A Aresto1964

      I have two classes like this public myclass1 { public int Id {set;get;) public myclass2 MyClass2{set;get;) } public myclass2 { public int Id {set;get;} public string Name {set;get;} } public list mylist = new List() ; my list has been bonded to datagridview . when displying the collection in the grid , the myclass1 is displaying the object name "DomaninName.myclass2" how to display the propery "Name" of myclass2

      J Offline
      J Offline
      joost versteegen
      wrote on last edited by
      #2

      you can overwrite the ToString function

      A 1 Reply Last reply
      0
      • A Aresto1964

        I have two classes like this public myclass1 { public int Id {set;get;) public myclass2 MyClass2{set;get;) } public myclass2 { public int Id {set;get;} public string Name {set;get;} } public list mylist = new List() ; my list has been bonded to datagridview . when displying the collection in the grid , the myclass1 is displaying the object name "DomaninName.myclass2" how to display the propery "Name" of myclass2

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        First, fix the compilation errors in your code: use of ')" where it should be '}', missing class keyword on class definitions, missing uppercase on List, that sort of thing. So us code that works to demonstrate your problem - not a lump of crap your threw together and didn't check at all ... You can display what you want as the cell content, just override ToStrign in your class:

            public class myclass2
                {
                public int Id { set; get; }
                public string Name { set; get; }
                public override string ToString()
                    {
                    return $"{Id}:{Name}";
                    }
                }
        

        You can also set the column header text to something more human readable than the property name - just set the HeaderText property of the column:

                myDataGridView.DataSource = mylist;
                myDataGridView.Columns\[1\].HeaderText = "My other class";
        

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony AntiTwitter: @DalekDave is now a follower!

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        1 Reply Last reply
        0
        • J joost versteegen

          you can overwrite the ToString function

          A Offline
          A Offline
          Aresto1964
          wrote on last edited by
          #4

          thank you it works fine for me .

          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