tags:

views:

112

answers:

2

I have created two classes:

public class Params : List<Param>
{

}

public class Param
{
    public enum enType
    {
        Integer,
        Double,
        String,
        DateTime
    }

    private string sName_m;
    public string Name
    {
        get { return sName_m; }
        set { sName_m = value; }
    }

    private string sValue_m;
    public string Value
    {
        get { return sValue_m; }
        set { sValue_m = value; }
    }

    private enType eType_m;
    public enType Type
    {
        get { return eType_m; }
        set { eType_m = value; }
    }
}

Now I want to be able to show the Params in a Grid type control on a windows application, so I dragged a DataGridView on to my form, and chose a datasouce by picking Other Data Sources -> Project Data Source, and then choosing my Params Class (frmMain+Params).

Now when I run the app, I can add/delete/edit records and the grid shows the three columns. What I'd like to be able to do is have the Type column be a drop down letting my pick values in the enumeration. Currently, I have to type a string that has to match the enumeration. Possible?

+1  A: 

I was never able to get the automatic data-binding to set up a DataGridViewComboBoxCell / DataGridViewComboBoxColumn properly (nor the CheckBox ones), so I resorted to setting AutoGenerateColumns to false and manually setting up the columns.

Not Sure
Ok, I removed the data source, and set up columns manually, I don't see an AutoGenerateColumns property on the DataGridView control.
Jeremy
That property is not available in the designer, you have to explicitly code it. Also, you don't need to remove the data source - data binding will still work with manually set up columns as long as you set the DataPropertyName and ValueType properties when you instantiate the column.
Not Sure
A: 

You can disable auto generation of the columns and manually generate the proper columns that you want, or you could remove the column and add a new one in its place.

var columns = dataGridView1.Columns;

var oldColumn = columns.Cast<DataGridViewColumn>()
                       .Single(c => c.DataPropertyName == "Type");
var index = columns.IndexOf(oldColumn);

var newColumn = new DataGridViewComboBoxColumn();
newColumn.Name = "Type";
newColumn.DataSource = Enum.GetValues(typeof(Param.enType));
newColumn.ValueType = typeof(Param.enType);

columns.RemoveAt(index);
columns.Insert(index, newColumn);
Samuel