Jeremy
Jeremy

Reputation: 46322

datagridview with comboboxes

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?

Upvotes: 0

Views: 821

Answers (2)

Samuel
Samuel

Reputation: 38346

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);

Upvotes: 1

Not Sure
Not Sure

Reputation: 5963

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.

Upvotes: 1

Related Questions