Tôi vừa mới có một usecase tương tự, và đã kết thúc viết một cái gì đó giống như mã bên dưới:
Viết di động tùy chỉnh và lớp Cột , và ghi đè lên các phương pháp EditType và InitializeEditingControl trên tế bào, để trở về điều khiển khác nhau cho phù hợp (ở đây tôi chỉ databinding vào một danh sách của một lớp tùy chỉnh với lĩnh vực .useCombo chỉ ra những gì kiểm soát để sử dụng):
// 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);
}
}
}
}
Sau đó, chỉ cần thêm một cột vào DataGridView của loại này và điều khiển chỉnh sửa chính xác sẽ là u sed.
Câu hỏi rất hay. Nó chỉ là vấn đề thời gian cho đến khi một người gặp vấn đề này khi sử dụng lớp DataGridView. – TheBlastOne