Search code examples
c#datagridviewdatagridviewcolumn

How to change cell type of a databound column


I have a datagridview that is bound to a dataset via a table adaptor which in turn linked to a SQL table.

enter image description here

I want to change the cell type of the status and priority columns to comboboxes.

I have tried this:

 private void dgvFechas_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
    {
        try
        {
            if (e.Column.ValueType == typeof(TextBox) && (e.Column.Index == 5) || (e.Column.Index == 6))
            {
                e.Column.CellTemplate = new DataGridViewComboBoxCell();
            }
        }
        catch (Exception ex) { }
    }

but it doesnt seem like the event handler fires when the program starts up.

So im not too sure how i can get it to work.


Solution

  • I followed this guide which allowed me change the cell type:

    https://social.msdn.microsoft.com/Forums/windows/en-US/c6d60712-135e-4fd3-a6a0-51dbb4be0dca/how-to-add-combobox-to-winform-datagridview-bound-to-datatable?forum=winforms

    After doing this i got a data_error which i fixed by ignoring the data error event. I know that isnt really the correct way of doing things, but it works for me.

     private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            //ignore
        }