Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Forms DataGridView control have different control types in the same column

Is it possible to do the following in a DataGridView:

In the same column I want to change the control type of each row between DataGridViewTextBoxColumn and DataGridViewComboBoxColumn?

(this is because sometimes I want to display a drop down list and other times I just want the user to enter a freehand value).

Thanks,

P.S. I am using C#

like image 789
MT. Avatar asked Jun 12 '09 11:06

MT.


People also ask

What is difference between DataGridView and DataGrid control in Winforms?

The DataGrid control is limited to displaying data from an external data source. The DataGridView control, however, can display unbound data stored in the control, data from a bound data source, or bound and unbound data together.

Which is a property of the DataGridView control?

The DataGridView control provides a customizable table for displaying data. The DataGridView class allows customization of cells, rows, columns, and borders through the use of properties such as DefaultCellStyle, ColumnHeadersDefaultCellStyle, CellBorderStyle, and GridColor.

How is a DataGridView control used?

The DataGridView control provides a powerful and flexible way to display data in a tabular format. You can use the DataGridView control to show read-only views of a small amount of data, or you can scale it to show editable views of very large sets of data.

Which of the following is the standard column type in the DataGrid?

The DataGridViewTextBoxColumn is a general-purpose column type for use with text-based values such as numbers and strings. In editing mode, a TextBox control is displayed in the active cell, enabling users to modify the cell value.


1 Answers

I've recently had a similar usecase, and ended up writing something like the below code:

Write a custom Cell and Column class, and override the EditType and InitializeEditingControl methods on the cell, to return different controls as appropriate (here I'm just databinding to a list of a custom class with .useCombo field indicating what control to use):

// Define a column that will create an appropriate type of edit control as needed.
public class OptionalDropdownColumn : DataGridViewColumn
{
    public OptionalDropdownColumn()
        : base(new PropertyCell())
    {
    }

    public override DataGridViewCell CellTemplate
    {
        get
        {
            return base.CellTemplate;
        }
        set
        {
            // Ensure that the cell used for the template is a PropertyCell.
            if (value != null &&
                !value.GetType().IsAssignableFrom(typeof(PropertyCell)))
            {
                throw new InvalidCastException("Must be a PropertyCell");
            }
            base.CellTemplate = value;
        }
    }
}

// And the corresponding Cell type
public class OptionalDropdownCell : DataGridViewTextBoxCell
{

    public OptionalDropdownCell()
        : base()
    {           
    }

    public override void InitializeEditingControl(int rowIndex, object
        initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
    {
        // Set the value of the editing control to the current cell value.
        base.InitializeEditingControl(rowIndex, initialFormattedValue,
            dataGridViewCellStyle);

        DataItem dataItem = (DataItem) this.OwningRow.DataBoundItem;
        if (dataItem.useCombo)
        {
            DataGridViewComboBoxEditingControl ctl = (DataGridViewComboBoxEditingControl)DataGridView.EditingControl;
            ctl.DataSource = dataItem.allowedItems;
            ctl.DropDownStyle = ComboBoxStyle.DropDownList;
        }
        else
        {
            DataGridViewTextBoxEditingControl ctl = (DataGridViewTextBoxEditingControl)DataGridView.EditingControl;
            ctl.Text = this.Value.ToString();
        }
    }

    public override Type EditType
    {
        get
        {
            DataItem dataItem = (DataItem)this.OwningRow.DataBoundItem;
            if (dataItem.useCombo)
            {
                return typeof(DataGridViewComboBoxEditingControl);
            }
            else
            {
                return typeof(DataGridViewTextBoxEditingControl);
            }
        }
    }
}

Then just add a column to your DataGridView of this type, and the correct edit control should be used.

like image 92
Brian Avatar answered Oct 26 '22 23:10

Brian