Combobox displayMembers with .dbml objects
-
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??
-
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??
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.
-
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.
-
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)??
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 -
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.
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/
-
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/
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 apartial class
definition. C# Partial Class[^]A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
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/
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.