4
votes

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.

1
what exactly do you need?...your datagrid is bound to a table and you add an unbound combobox column mapping to a enum.After this what do you need,for example selected item in combobox map to some value in one of the columns? - terrybozzio
I've a grid which is bound to a dataset. - Visée Maxence
yes ,you are right.by knowing the grid is already bound to a dataset... I just need to know how to bind the datagridviewcomboboxcolumn (the value in DB is the index of the combobox. and the datasource of the combobox is an Enum). - Visée Maxence
ahhh i think i got you right,you have a column with some values and the unbound datagridviewcomboboxcolumn you want that the selected value in each combobox be equal to the value of that rows column from the database right? for example imagine your database column is named "test",in rowindex 2 column["test"] the value is 2 and in that same row but in the combobox column the displayed value be index 2 of the items there?....let me know if it is so i present some option. - terrybozzio
yes , that's what I need :) - Visée Maxence

1 Answers

4
votes

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
}