> 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