C # -- DataGridView Add the DateTimePicker time control,

Source: Internet
Author: User

C # -- DataGridView Add the DateTimePicker time control,
Using System; using System. collections. generic; using System. componentModel; using System. data; using System. drawing; using System. linq; using System. text; using System. windows. forms; namespace Test {public partial class Form1: Form {DateTimePicker dtp = new DateTimePicker (); // instantiate a DateTimePicker control Rectangle _ Rectangle; public Form1 () {InitializeComponent (); dataGridView1.Controls. add (dtp); // Add the time control to the DataGridView dtp. visible = false; // do not display dtp first. format = DateTimePickerFormat. custom; // set the date format, dtp. textChanged + = new EventHandler (dtp_TextChange ); // Add the event dtp_TextChange}/**************** when the time control is selected ************ * ***/private void dtp_TextChange (object sender, eventArgs e) {maid. value = dtp. text. toString (); // when the time control is selected, grant the time content to the cell}/****************** the cell is clicked, determines whether the column of the time control is *******************/private void datagridviewinclucellclick (object sender, DataGridViewCellEventArgs e) {if (e. columnIndex = 0) {_ Rectangle = maid (e. columnIndex, e. rowIndex, true); // obtain the location and size of the cell dtp. size = new Size (_ Rectangle. width, _ Rectangle. height); // assign the cell size to the time control dtp. location = new Point (_ Rectangle. x, _ Rectangle. y); // assign the cell position to the time control dtp. visible = true; // display control} else dtp. visible = false;}/*********** when the column width changes, the time control is hidden first, otherwise, the time control cannot be changed when the cell width and height change. */private void dataGridView1_ColumnWidthChanged (object sender, DataGridViewColumnEventArgs e) {dtp. visible = false;}/*********** when the scroll bar is rolling, the cell position changes, and the time control is also hidden, otherwise, the time control location will be messy. *******/private void dataGridView1_Scroll (object sender, ScrollEventArgs e) {dtp. visible = false ;}}}

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.