views:

2468

answers:

2

If you have a DataTable that has a column of some Enum type.

And you bind a DataGridView to this DataTable (myDgv.DataSource = myDataTable)..

How can you make the DataGridView show a combobox (or is it drop-down-list? The one where the only thing you can do is select) in each cell of for this column? The combobox should have the current value selected, and the other possible enum values selectable.

At the moment, these cells show as plain old editable text cells with the string representation of the enum value in them.

+1  A: 

I would suggest that you read Forcing the DataGridView to do my bidding - a tale of ComboBox hackery:

At first I was optimistic about the DataGridView samples (overview)(Download The DataGridView samples), but I didn't see anything that did what I wanted to do: take an enum value and represent it in the grid with a combobox. So, here's how I did it.

Also you should check out How to: Bind Objects to Windows Forms DataGridView Controls:

The following code example demonstrates how to bind a collection of objects to a DataGridView control so that each object displays as a separate row. This example also illustrates how to display a property with an enumeration type in a DataGridViewComboBoxColumn so that the combo box drop-down list contains the enumeration values.

Andrew Hare
+1  A: 

Well, I don't know if what I'm going to say fits here, but I recently had a similar requirement: display a Link in a DataGridView bound to a DataSet, this is

protected void grvResultado_RowDataBound(object sender, GridViewRowEventArgs e) {
    if (grvResultado.HeaderRow == null || grvResultado.HeaderRow.Cells.Count == 0) return;
    bool hasLink = false;
    int ind = 0;
    foreach (TableCell c in grvResultado.HeaderRow.Cells) {
     if (c.Text == "link") {
      hasLink = true;
      break;
     }
     ind++;
    }
    if (!hasLink) return;


    if (e.Row.RowType == DataControlRowType.DataRow) {
     TableCell c = e.Row.Cells[ind];
     var lnk = new HyperLink();
     lnk.Text = "Ver";
     lnk.NavigateUrl = c.Text;
     c.Controls.Clear();
     c.Controls.Add(lnk);
    }
}

You can accustom what you need as I did

Jhonny D. Cano -Leftware-