C # dview ComboBox editable

Source: Internet
Author: User

Add the following two classes to the same namespace of form1:

# Region C # dview ComboBox editable
// The class to be added
Public class datagridviewcomboeditboxcolumn: datagridviewcomboboxcolumn
{
Public datagridviewcomboeditboxcolumn ()
{
Datagridviewcomboeditboxcell OBJ = new datagridviewcomboeditboxcell ();
This. celltemplate = OBJ;
}
}

// The class to be added
Public class datagridviewcomboeditboxcell: datagridviewcomboboxcell
{
Public override void initializeeditingcontrol (INT rowindex, object initialformattedvalue, datagridviewcellstyle)
{
Base. initializeeditingcontrol (rowindex, initialformattedvalue, datagridviewcellstyle );

ComboBox = (ComboBox) base. datagridview. editingcontrol;

If (ComboBox! = NULL)
{
ComboBox. dropdownstyle = comboboxstyle. dropdown;
ComboBox. autocompletemode = autocompletemode. Suggest;
ComboBox. Validating + = new canceleventhandler (combobox_validating );
}
}

Protected override object getformattedvalue (object value, int rowindex, ref datagridviewcellstyle cellstyle, typeconverter valuetypeconverter, typeconverter formattedvaluetypeconverter, datagridviewdataerrorcontexts context)
{
If (value! = NULL)
{
If (value. tostring (). Trim ()! = String. Empty)
{
If (items. indexof (value) =-1)
{
Items. Add (value );
Datagridviewcomboboxcolumn Col = (datagridviewcomboboxcolumn) owningcolumn;
Col. Items. Add (value );
}
}
}
Return base. getformattedvalue (value, rowindex, ref cellstyle, valuetypeconverter, formattedvaluetypeconverter, context );
}

Void combobox_validating (Object sender, system. componentmodel. canceleventargs E)
{
Datagridviewcomboboxeditingcontrol CBO = (datagridviewcomboboxeditingcontrol) sender;
If (CBO. Text. Trim () = string. Empty) return;

Datagridview grid = CBO. editingcontroldatagri;
Object value = CBO. text;

// Add Value to list if not there
If (CBO. Items. indexof (value) =-1)
{
Datagridviewcomboboxcolumn cbocol = (datagridviewcomboboxcolumn) grid. Columns [grid. currentcell. columnindex];
// Must add to both the current ComboBox as well as the template, to avoid duplicate entries
CBO. Items. Add (value );
Cbocol. Items. Add (value );
Grid. currentcell. value = value;
}
}
}
# Endregion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.