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. Combobox displayMembers with .dbml objects

Combobox displayMembers with .dbml objects

Scheduled Pinned Locked Moved C#
csharpdatabaselinqquestion
7 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.
  • G Offline
    G Offline
    grmihel2
    wrote on last edited by
    #1

    Are there any genius way to make Display and value member object related to the object in the combobox? As you can see in the following code, I'm using IEnumrable of Email objects to add into the combobox, and the Email are an entity object related to the LinQ (.dbml) object mapped from the database (which means each object itself contains value fields from the database). comboBox_n1.Items.Clear(); comboBox_n1.Items.AddRange(_controller.getEmailList().ToArray<Email>()); comboBox_n1.Sorted = true; comboBox_n1.SelectedIndex = -1; Right now it shows an object string like: 'MyApp.Email' for each object in the list. Before now, I have made a ToString() in the .dbml code, for each object which ofcause works, but it ain't a sustainable solution since it will dissapear each time I made a change to the datamapper. So are there any way to auto generate these display members??

    P 1 Reply Last reply
    0
    • G grmihel2

      Are there any genius way to make Display and value member object related to the object in the combobox? As you can see in the following code, I'm using IEnumrable of Email objects to add into the combobox, and the Email are an entity object related to the LinQ (.dbml) object mapped from the database (which means each object itself contains value fields from the database). comboBox_n1.Items.Clear(); comboBox_n1.Items.AddRange(_controller.getEmailList().ToArray<Email>()); comboBox_n1.Sorted = true; comboBox_n1.SelectedIndex = -1; Right now it shows an object string like: 'MyApp.Email' for each object in the list. Before now, I have made a ToString() in the .dbml code, for each object which ofcause works, but it ain't a sustainable solution since it will dissapear each time I made a change to the datamapper. So are there any way to auto generate these display members??

      P Offline
      P Offline
      phil o
      wrote on last edited by
      #2

      Hi, You can put your ToString() method in a separate file, making use of the 'partial' keyword in the class declaration. So, when you regenerate your model, your separate file won't be impacted by the changes, therefore your tweak will still work. Hope this helps. Regards.

      G 2 Replies Last reply
      0
      • P phil o

        Hi, You can put your ToString() method in a separate file, making use of the 'partial' keyword in the class declaration. So, when you regenerate your model, your separate file won't be impacted by the changes, therefore your tweak will still work. Hope this helps. Regards.

        G Offline
        G Offline
        grmihel2
        wrote on last edited by
        #3

        Thanks, that will work for sure. But it still feels like some kind of 'hack' in the structure. Ain't there any official way to do it, like 'click here and choose which data field to be shown as display member', for non-DataSources (like generic lists)??

        D 1 Reply Last reply
        0
        • G grmihel2

          Thanks, that will work for sure. But it still feels like some kind of 'hack' in the structure. Ain't there any official way to do it, like 'click here and choose which data field to be shown as display member', for non-DataSources (like generic lists)??

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          grmihel2 wrote:

          Ain't there any official way to do it, like 'click here and choose which data field to be shown as display member'

          No, there is no "offical" way to do it. Based on your requirements, the suggestion given is the best one to go with. Use a Partial class and put the code that cannot change into a seperate file.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak

          1 Reply Last reply
          0
          • P phil o

            Hi, You can put your ToString() method in a separate file, making use of the 'partial' keyword in the class declaration. So, when you regenerate your model, your separate file won't be impacted by the changes, therefore your tweak will still work. Hope this helps. Regards.

            G Offline
            G Offline
            grmihel2
            wrote on last edited by
            #5

            Just realized that I've never used the partial function before, so I'm not sure how to attack it. Could you gief an example plz? I have a CRUD (Create, Read, Update, Delete) class for handeling all the email specific methods like: public Email GetAllEmails(){} The Email class is within the DataClass Datacontext, and thats the one who is getting regenerated each time I have a db scheme change. Could you give an example of how to make this partial class for a ToString() for the Email class? Here is a short outtake of the CD (Class Diagram): http://imageshack.us/photo/my-images/36/classdiagram.jpg/

            D P 2 Replies Last reply
            0
            • G grmihel2

              Just realized that I've never used the partial function before, so I'm not sure how to attack it. Could you gief an example plz? I have a CRUD (Create, Read, Update, Delete) class for handeling all the email specific methods like: public Email GetAllEmails(){} The Email class is within the DataClass Datacontext, and thats the one who is getting regenerated each time I have a db scheme change. Could you give an example of how to make this partial class for a ToString() for the Email class? Here is a short outtake of the CD (Class Diagram): http://imageshack.us/photo/my-images/36/classdiagram.jpg/

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              Simple. It's a class that get split into multiple files. One of them has to have the normal class definition. The others all continue the class code inside a partial class definition. C# Partial Class[^]

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak

              1 Reply Last reply
              0
              • G grmihel2

                Just realized that I've never used the partial function before, so I'm not sure how to attack it. Could you gief an example plz? I have a CRUD (Create, Read, Update, Delete) class for handeling all the email specific methods like: public Email GetAllEmails(){} The Email class is within the DataClass Datacontext, and thats the one who is getting regenerated each time I have a db scheme change. Could you give an example of how to make this partial class for a ToString() for the Email class? Here is a short outtake of the CD (Class Diagram): http://imageshack.us/photo/my-images/36/classdiagram.jpg/

                P Offline
                P Offline
                phil o
                wrote on last edited by
                #7

                Hi, Sorry for this late reply but I was on vacation these days. First, you have to make sure that generated classes are all defined with partial keyword. For example :

                // Auto-generated file
                public partial class DataClass
                {
                //...

                public partial class Email
                {
                //...
                }

                //...
                }

                Then, just add a Whatever.cs file to your project :

                // Custom file. This will never be modified when you regenerate your model.
                public partial class DataClass
                {
                //...

                public partial class Email
                {
                public override string ToString()
                {
                //...
                }
                }

                //...
                }

                You could also define an extension method for you Email class : just add an Extensions.cs file to your project and define an extension method to the Email class.

                public static class Extensions
                {
                public static string ToString(this Email email)
                {
                //...
                }
                }

                Hope this helps. Kindly.

                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