Display enum in ComboBox with spaces

G

Gerrit

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
 
A

Alberto Poblacion

Gerrit said:
[...]
enum MyEnum
{
My_Value_1,
My_Value_2
}
[...]
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

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=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.
 
G

Gerrit

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=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.


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 = 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());
}

}
 
A

Alberto Poblacion

Gerrit said:
Thanks for your answer, but I have an problem with it.
[...]
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?

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.
 
G

Gerrit

Alberto Poblacion said:
news:ls9mi.464$Rv4.84@amstwist00...
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.

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
 
A

Andrew

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
 
A

Andrew

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
 
A

Andrew Bitzer

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top