Visée Maxence
Visée Maxence

Reputation: 249

Databinding DatagridviewComboboxColumn with enum

I've a datagridview which is populated by getting data from SQL server.

gridview.Datasource = dataSet.Tables[0] 

-> No problem with that.

In this grid , one column is a ComboboxColumn...

For the filling (I mean, to bind a datasource only ) of this one , no problem :

cmb.DataSource = Enum.GetValues(typeof(MyEnum));
cmb.ValueType = typeof(MyEnum);
cmb.DataPropertyName = "MyEnum";

I would like to know how to data bind the datagridviewcomboboxcolumn (the value in DB for this column is the index of the selected value for this combobox. and the datasource of this combobox is an Enum).

Value in DB : 2 -> which is the index of the item to display

May I to precise the DB column name somewhere ? If I do that in the datapropertyname I get an error -> DataGridViewComboboxCell value is not valid ...

Thanks in advance !

edit : problem "solved". I replaced the Enum with a datatable. much simpler.

Upvotes: 4

Views: 9463

Answers (1)

terrybozzio
terrybozzio

Reputation: 4542

As stated in the comments above i present you with the following option:

This is as an example you can fine-grained this as you see fit,so first before you set the gridview datasource add the unbound comboboxColumn give it a name, then set datasource,then set the datagridview datasource and subscribe for example for the CellEndEdit and RowStateChanged event like this:

 DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
 col.DataSource = Enum.GetValues(typeof(MyEnum));
 col.Name = "testcolumn";

 int index = dataGridView1.Columns.Add(col);
 //"index" is if you want to set properties and so on to this column
 //but no need for this example.
 //dataGridView1.Columns[index].Name = "testcolumn";

 dataGridView1.DataSource = test;

 //the 2 event-handlers         
 dataGridView1.CellEndEdit += new DataGridViewCellEventHandler(dataGridView1_CellEndEdit);
 dataGridView1.RowStateChanged += new DataGridViewRowStateChangedEventHandler(dataGridView1_RowStateChanged);

Then inside these 2 handlers do this(the CellEndEdit is handled so every time you edit the cell containing the values from the database the comboboxcell updates as well);

void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{   
      //here i use TestCol as a name for the column i want to check
      //you replace it with the column you have from the database
      //if you didnt change it of course...
      if (e.ColumnIndex == dataGridView1.Columns["TestCol"].Index)
      {
           //and here i assign the value on the current row in the testcolumn column
           //thats the combobox column...
           dataGridView1.Rows[e.RowIndex].Cells["testcolumn"].Value = (MyEnum)((int)dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);
      }
}

private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
{
      //here i assign the initial values to the each cell in the combobox column
      //this whole example could be done in other ways but in a nutshell
      //it should provide you a good kickstart to play around.
      if (e.Row.DataBoundItem != null)
      {
           e.Row.Cells["testcolumn"].Value = (MyEnum)((int)e.Row.Cells["TestCol"].Value);
      }
}

i assume here that the column from the database have only numeric values like 0 or 2 or 5,and your enum must have the same amount of values for example,if in the database column your values go up to the maximum of 5 then your enum will be like this for example:

public enum MyEnum
{
    zero,
    one,
    two,
    three,
    four,
    five
}

Upvotes: 4

Related Questions