मैं हाल ही में एक ऐसी ही USECASE लिया है सेल टेम्पलेट के रूप में इस वर्ग का एक उदाहरण के साथ एक DataGridViewColumn बना सकते हैं और नीचे दिए गए कोड की तरह कुछ लिखने के समाप्त हो गया:
एक कस्टम सेल और कॉलम वर्ग लिखें , और सेल पर EditType और InitializeEditingControl तरीकों को ओवरराइड, उचित रूप में विभिन्न नियंत्रणों वापस जाने के लिए (यहाँ मैं सिर्फ क्या उपयोग करने के लिए नियंत्रण का संकेत .useCombo क्षेत्र के साथ एक कस्टम वर्ग की एक सूची के लिए डेटा बाइंडिंग कर रहा हूँ):
// 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);
}
}
}
}
फिर बस इस प्रकार के अपने डेटाग्रिड व्यू में एक कॉलम जोड़ें, और सही संपादन नियंत्रण आपको होना चाहिए एसईडी।
बहुत अच्छा सवाल है। डेटाग्रिड व्यू क्लास का उपयोग करते समय यह किसी समस्या का सामना करने तक ही समय की बात है। – TheBlastOne