Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / July 2007

Tip: Looking for answers? Try searching our database.

Display enum in ComboBox with spaces

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Gerrit - 14 Jul 2007 09:34 GMT
Hello,

I have an enum, example:

enum MyEnum
{
   My_Value_1,
   My_Value_2
}

With :

comboBox1.DataSource = Enum.GetValues(typeof(MyEnum));

I fill the ComboBox Items and with

DataGridViewComboBoxColumn col =
(DataGridViewComboBoxColumn)dataGridView1.Columns[0];
col.DataSource = Enum.GetValues(typeof(EnumNamen));

I can fill a DataBound DataGridViewComboBoxColumn with items.

But now my question: How can I replace the "_" with " " so that I became
items with spaces instead of underscores? And that a databound object still
works

Thanks a lot for your help

Gerrit
Alberto Poblacion - 14 Jul 2007 10:58 GMT
> [...]
> enum MyEnum
[quoted text clipped - 8 lines]
> items with spaces instead of underscores? And that a databound object
> still works

  You could loop through all the elements, replace the "_", and insert them
into an array, and the databind to your array. For instance, like this:

Array a = Enum.GetValues(typeof(MyEnum));
string[] myValues = new string[a.Length];
for (int i=0; i<a.Length; i++)
{
   myValues[i]=a.GetValue(i).ToString().Replace("_"," ");
}

 If you are going to do many such conversions, you can experiment a little
bit and do the conversion in a single line by means of Array.ConvertAll(),
providing as an argument a Converter that you write separately to perform
the replacement.
Gerrit - 14 Jul 2007 20:27 GMT
>   You could loop through all the elements, replace the "_", and insert
> them into an array, and the databind to your array. For instance, like
[quoted text clipped - 11 lines]
> providing as an argument a Converter that you write separately to perform
> the replacement.

Thanks for your answer, but I have an problem with it. You can find it in
the following code (from the Microsoft Help)

In this code I have placed your code in 'DataGridViewComboBoxColumn
CreateComboBoxWithEnums()'

When running this code there is an error that the value of the
DataGridViewComboBoxCell is invalid.

This is because The King is different from The_King. Is there a solution for
this problem?

The code:

using System;
using System.Windows.Forms;

public enum Title
{
   The_King,
   A_Sir
};

public class EnumsAndComboBox : Form
{
   private DataGridView dataGridView1 = new DataGridView();
   private BindingSource bindingSource1 = new BindingSource();

   public EnumsAndComboBox()
   {
       this.Load += new System.EventHandler(EnumsAndComboBox_Load);
   }

   private void EnumsAndComboBox_Load(object sender, System.EventArgs e)
   {
       // Populate the data source.
       bindingSource1.Add(new Knight(Title.The_King, "Uther", true));
       bindingSource1.Add(new Knight(Title.The_King, "Arthur", true));
       bindingSource1.Add(new Knight(Title.A_Sir, "Mordred", false));
       bindingSource1.Add(new Knight(Title.A_Sir, "Gawain", true));
       bindingSource1.Add(new Knight(Title.A_Sir, "Galahad", true));

       // Initialize the DataGridView.
       dataGridView1.AutoGenerateColumns = false;
       dataGridView1.AutoSize = true;
       dataGridView1.DataSource = bindingSource1;

       dataGridView1.Columns.Add(CreateComboBoxWithEnums());

       // Initialize and add a text box column.
       DataGridViewColumn column = new DataGridViewTextBoxColumn();
       column.DataPropertyName = "Name";
       column.Name = "Knight";
       dataGridView1.Columns.Add(column);

       // Initialize and add a check box column.
       column = new DataGridViewCheckBoxColumn();
       column.DataPropertyName = "GoodGuy";
       column.Name = "Good";
       dataGridView1.Columns.Add(column);

       // Initialize the form.
       this.Controls.Add(dataGridView1);
       this.AutoSize = true;
       this.Text = "DataGridView object binding demo";
   }

   DataGridViewComboBoxColumn CreateComboBoxWithEnums()
   {
// Here is your code
// ------------------
       Array a = Enum.GetValues(typeof(Title));
       string[] myValues = new string[a.Length];
       for (int i = 0; i < a.Length; i++)
       {
           myValues[i] = a.GetValue(i).ToString().Replace("_", " ");
       }

       DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
       //combo.DataSource = Enum.GetValues(typeof(Title));
// And here too...
       combo.DataSource = myValues;
       combo.DataPropertyName = "Title";
       combo.Name = "Title";
       return combo;
   }
   #region "business object"
   private class Knight
   {
       private string hisName;
       private bool good;
       private Title hisTitle;

       public Knight(Title title, string name, bool good)
       {
           hisTitle = title;
           hisName = name;
           this.good = good;
       }

       public Knight()
       {
           hisTitle = Title.A_Sir;
           hisName = "<enter name>";
           good = true;
       }

       public string Name
       {
           get
           {
               return hisName;
           }

           set
           {
               hisName = value;
           }
       }

       public bool GoodGuy
       {
           get
           {
               return good;
           }
           set
           {
               good = value;
           }
       }

       public Title Title
       {
           get
           {
               return hisTitle;
           }
           set
           {
               hisTitle = value;
           }
       }
   }
   #endregion

   [STAThread]
   public static void Main()
   {
       Application.Run(new EnumsAndComboBox());
   }

}
Alberto Poblacion - 14 Jul 2007 21:11 GMT
> Thanks for your answer, but I have an problem with it.
> [...]
[quoted text clipped - 3 lines]
> This is because The King is different from The_King. Is there a solution
> for this problem?

  Ah, I see. I don't think that there is a clean and easy solution. You are
not only using the databinding to display a list of strings in a combo, but
you are then databinding the combo to a property of an object that is of the
type of the Enum. This is not going to work, since now your combo contains
strings rather than the values of the Enum.

  You could do something a little more complicated where you create a class
that contains the enum and an override for ToString() to present the text,
and fill the combo with objects of this class, but then you would have to
modify your Knight class to use this class rather than the Enum in order to
perform the databinding. It will probably need some additional work such as
overriding Equals in the class so that the databinding can find the values.
All in all, it's going to get ugly. I hope someone else proposes a cleaner
solution.
Gerrit - 15 Jul 2007 12:07 GMT
>   Ah, I see. I don't think that there is a clean and easy solution. You
> are not only using the databinding to display a list of strings in a
[quoted text clipped - 10 lines]
> find the values. All in all, it's going to get ugly. I hope someone else
> proposes a cleaner solution.

Yes, I hope so too. Mayby have others an other solution for working with
enums where the combobox must show different text then the enum items? I saw
a enum typeconverter on
http://www.codeproject.com/csharp/EnumDescConverter.asp, but I don 't know
if I can use is or how I can use it in my situation.

Gerrit
Andrew - 23 Jul 2007 11:06 GMT
Hi,

Have a look at the BindingSource-Property of the Gridview. You can register for the format- and parse-events. In eventhandlers you can place the code to do all the necessary stuff to show nice values.

Hope it works,
Andrew

EggHeadCafe.com - .NET Developer Portal of Choice
http://www.eggheadcafe.com
Andrew - 23 Jul 2007 11:07 GMT
Hi,

Have a look at the BindingSource-Property of the Gridview. You can register for the format- and parse-events. In eventhandlers you can place the code to do all the necessary stuff to show nice values.

Hope it works,
Andrew

EggHeadCafe.com - .NET Developer Portal of Choice
http://www.eggheadcafe.com
Andrew Bitzer - 23 Jul 2007 11:45 GMT
Hi,

Have a look at the BindingSource-Property of the Gridview. You can register for the format- and parse-events. In eventhandlers you can place the code to do all the necessary stuff to show nice values.

Hope it works,
Andrew

EggHeadCafe.com - .NET Developer Portal of Choice
http://www.eggheadcafe.com

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.