Host the control in a Windows form datagridview Cell

Source: Internet
Author: User

datagridview Control provides multiple column types, allowing you to enter and Edit values in multiple ways. However, if these column types cannot meet the data input requirements, you can also use cells that hold the selected control to create your own column types. To do this, you must define the derived from datagridviewcolumn and datagridviewcell class. You must also define the derived from Control and implement idatagridvieweditingcontrol interface.

The following Code example shows how to create a calendar column. The cells in this column display dates in common text box cells. However, when you edit a cell, datetimepicker control. To avoid text box display, calendarcell class from

Note:

WhenDatagridviewcellOrDatagridviewcolumnWhen deriving and adding new attributes to a derived class, make sure to overrideCloneMethod To copy new attributes during the clone operation. The base classCloneTo copy the attributes of the base class to a new cell or column.

ExampleC #

 Using System;Using System. Windows. forms; Public   Class Calendarcolumn: datagridviewcolumn { Public Calendarcolumn (): Base ( New Calendarcell ()){} Public Override datagridviewcell celltemplate { Get { Return   Base . Celltemplate ;} Set {// Ensure that the cell used for the template is a calendarcell.              If (Value! = Null &&! Value. GetType (). isassignablefrom (typeof (calendarcell) {Throw New Invalidcastexception ( "Must be a calendarcell" );} Base . Celltemplate = value ;}}} Public   Class Calendarcell: Maid { Public Calendarcell ():Base (){ // Use the short date format.          This . Style. format = "D" ;} Public Override Void Initializeeditingcontrol ( Int Rowindex, object initialformattedvalue, maid ){ // Set the value of the editing control to the current cell value.          Base . Initializeeditingcontrol (rowindex, initialformattedvalue, datagridviewcellstyle); calendareditingcontrol CTL = datagridview. editingcontrol as calendareditingcontrol; CTL. value = (datetime) This . Value ;} Public Override type edittype { Get { // Return the type of the editing contol that calendarcell uses.              Return Typeof (calendareditingcontrol );}} Public Override type valuetype { Get {// Return the type of the value that calendarcell contains.              Return Typeof (datetime );}} Public Override object defaultnewrowvalue { Get { // Use the current date and time as the default value.              Return Datetime. Now ;}}} Class Calendareditingcontrol: datetimepicker, idatagridvieweditingcontrol {datagridview datagri; Private   Bool Valuechanged = False ; Int Rowindex; Public Calendareditingcontrol (){ This . Format = datetimepickerformat. Short ;} // Implements the idatagridvieweditingcontrol. editingcontrolformattedvalue      // Property.      Public Object editingcontrolformattedvalue { Get { Return   This . Value. tow.datestring ();} Set {If (Value is string ){ This . Value = datetime. parse (string) value );}}} // Implements      // Idatagridvieweditingcontrol. geteditingcontrolformattedvalue method.      Public Object geteditingcontrolformattedvalue (maid context ){ Return Editingcontrolformattedvalue ;} // Implements      // Idatagridvieweditingcontrol. applycellstyletoeditingcontrol method.      Public  Void Applycellstyletoeditingcontrol (maid ){ This . Font = maid. Font; This . Calendarforecolor = maid. forecolor; This . Calendarmonthbackground = maid. backcolor ;} // Implements the idatagridvieweditingcontrol. editingcontrolrowindex      // Property.      Public   Int Editingcontrolrowindex { Get {Return Rowindex ;} Set {Rowindex = value ;}} // Implements the idatagridvieweditingcontrol. editingcontrolwantsinputkey      // Method.      Public   Bool Editingcontrolwantsinputkey (Keys key, Bool Datagridviewwantsinputkey ){ // Let the datetimepicker handle the keys listed.          Switch (Key & keys. keycode ){ Case Keys. left:Case Keys. Up: Case Keys. down: Case Keys. Right: Case Keys. Home: Case Keys. end: Case Keys. Pagedown: Case Keys. Pageup: Return   True ; Default : Return ! Datagridviewwantsinputkey ;}}// Implements the idatagridvieweditingcontrol. prepareeditingcontrolforedit      // Method.      Public   Void Prepareeditingcontrolforedit ( Bool Selectall ){ // No preparation needs to be done. } // Implements the idatagridvieweditingcontrol      //. Repositioneditingcontrolonvaluechange property.      Public   Bool Repositioneditingcontrolonvaluechange { Get {Return   False ;}} // Implements the idatagridvieweditingcontrol      //. Editingcontroldatagridview property.      Public Datagridview editingcontroldatagridview { Get { Return Datagridview ;} Set {Datagridview = value ;}} // Implements the idatagridvieweditingcontrol      //. Editingcontrolvaluechanged property.     Public   Bool Editingcontrolvaluechanged { Get { Return Valuechanged ;} Set {Valuechanged = value ;}} // Implements the idatagridvieweditingcontrol      //. Editingpanelcursor property.      Public Cursor editingpanelcursor { Get { Return   Base . Cursor ;}}Protected Override Void Onvaluechanged (eventargs ){ // Define y the datagridview that the contents of the cell          // Have changed. Valuechanged = True ; This . Editingcontroldatagridatagri. policycurrentcelldirty ( True ); Base . Onvaluechanged (eventargs );}} Public   Class Form1: FORM { Private Datagridview datagridatagri1 = New Datagridview (); [stathreadattribute ()] Public   Static   Void Main () {application. Run ( New Form1 ());} Public Form1 (){ This . Maid = dockstyle. Fill; This . Controls. Add ( This . Datagridview1 ); This . Load + = New Eventhandler (form1_load ); This . Text = "Datagridview calendar column Demo" ;} Private   Void Formateload (Object sender, eventargs e) {calendarcolumn Col = New Calendarcolumn (); This . Maid. Add (COL ); This . Maid = 5; Foreach (Datagridviewrow row In   This . Datagridview1.rows) {row. cells [0]. value = datetime. Now ;}}}

Related Article

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.