DataGridViewComboBoxColumn and Enum values

Hi,

I'm trying to use a ComboBoxColumn in a DataGridView to allow editing of an enum value in a class. E.g



public enum Stuff
{
   Apples,
   Pears,
   Oranges
}

public class MyTestClass
{
   public Stuff MyStuff { get { return m_MyStuff; } set { m_MyStuff = value; } }
   public string MyString { get { return m_MyString; } set { m_MyString = value; } }

   private Stuff m_MyStuff = Stuff.Apples;
   private string m_MyString = "Test";

   public test() {  }
}

 


So I want to use a DataGridView to display a BindingList<MyTestClass> where the first column is a text column and the second is a drop down that lets the user select the type of "Stuff" they want.

I've tried for a long time to get this to work but without any luck! I tried manually setting the .Items property of the drop down column to all of the values in "Stuff", which results in value not found exceptions and various other issues. I've tried creating a dataTable that contains a row for each of the values in the Stuff Enum but, again, more value not found and other exceptions.

Is there a simple way to get this to work the way I want I'm using a binding source for the data grid view which is bound to MyTestClass.

Thanks,

Koni


Answer this question

DataGridViewComboBoxColumn and Enum values

  • rhaazy

    There just happens to be a help topic on binding the DataGridView combobox column to enum values: http://msdn2.microsoft.com/en-us/library/y0wfd4yz.aspx I don't know if it works with Beta2. There were a lot of bugs fixed since then.
    (EDIT: updated link)

    -mark
    DataGridView Program Manager
    Microsoft
    This post is provided "as-is"


  • vijay_jan80

    You may want to try somehting like this:

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

  • Greg Wishart

    I've managed to get it to work by manually adding the enum values to the combo box with Items.Add(). I was getting an exception, but it was caused by having FullRowSelect property set, so I switched to cell select mode and it works fine now.

    BTW this is with Beta2.

  • Reaper069

     Mark Rideout wrote:

    There just happens to be a help topic on binding the DataGridView combobox column to enum values: http://msdn2.microsoft.com/en-us/library/fbk67b6z.aspx. I don't know if it works with Beta2. There were a lot of bugs fixed since then."



    Hi Mark, I checked out that article but It doesn't appear to be related to combo grid elements and enum values, is the link correct

    Thanks

  • e2e

    I've updated the link in my post. Sorry about that. http://msdn2.microsoft.com/en-us/library/y0wfd4yz.aspx

    -mark
    DataGridView program Manager
    Microsoft
    This post is provided "as-is"

  • Moinuddin

     Mike Danes wrote:
    You may want to try somehting like this:

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


    Thanks Mike, this cuts out a few lines of code and is much neater than looping through the enum values and adding them by hand.

  • DataGridViewComboBoxColumn and Enum values