Datetimepicker inserted in the datetime column of the datagridview Control

Source: Internet
Author: User
Public form1 ()
...{
Initializecomponent ();
}

// Define a datetimepicker Control
Private datetimepicker dtimepicker = new datetimepicker ();

// Define a dataset
Private dataset Ds;

Private void form1_load (Object sender, eventargs E)
...{
// Hide dtimepicker
Dtimepicker. Visible = false;

// Datagirdview1 Control Data Binding
Data_bind ();


// Define the control dtimepicker. when the text is changed, it is delegated to the event dtimepicker_textchanged.
Dtimepicker. textchanged + = new eventhandler (dtimepicker_textchanged );

// Add the dtimepicker control to the datagridview1
Datagridview1.controls. Add (dtimepicker );
}

/** // <Summary>
/// Bind irdview to data
/// </Summary>
Private void data_bind ()
...{
String sqlstr = "select businessname, lastlogindate from business ";
DS = sqlhelper. executeds (commandtype. Text, sqlstr );
Datagridview1.datasource = Ds. Tables [0]. defaultview;
}


// Event dtimepicker_textchanged, triggered when dtimepicker changes text
Private void dtimepicker_textchanged (Object sender, eventargs E)
...{
// Assign the text value of dtimepicker to the selected cell value of datagridview1
Datagridview1.currentcell. value = dtimepicker. text;
}

Private void datagridview1_scroll (Object sender, scrolleventargs E)
...{
// Hide the dtimepicker control when the dview1 scroll bar moves
Dtimepicker. Visible = false;
}

Private void datagridview1_columnwidthchanged (Object sender, datagridviewcolumneventargs E)
...{
// When the column width of datagridview1 changes, the dtimepicker control is hidden.
Dtimepicker. Visible = false;

}

Private void maid cellclick (Object sender, maid E)
...{
// Determine the date type of the selected row
// DT = Ds. Tables [0];
Type T = Ds. Tables [0]. Columns [datagridview1.currentcell. columnindex]. datatype;
Datetime d = datetime. now;
If (t = D. GetType ())
...{
// Obtain the rectangle of the display area of the selected cell in datagridview1
Rectangle rect = This. Maid. getcelldisplayrectangle (this. Maid. currentcell. columnindex, this. Maid. rowindex, false );
// Dtimepicker is displayed in the rectangle of the display area of the selected cell in datagridview1, that is, within the selected cell
Dtimepicker. Visible = true;
Dtimepicker. Top = rect. Top;
Dtimepicker. Left = rect. Left;
Dtimepicker. Height = rect. height;
Dtimepicker. width = rect. width;
// The dtimepicker control obtains the value of the selected cell in the initial value of datagridview1.
Dtimepicker. Text = maid. value. tostring ();

}
}

~ Form1 ()
...{
DS. Dispose ();
DS. Clear ();
System. gc. Collect ();
}


}

 

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.