[CSHARP] view plaincopyprint? Datetimepicker DTP = new datetimepicker ();

Source: Internet
Author: User

Http://blog.csdn.net/diyoosjtu/article/details/7584857

 

Because the columntype provided by the datagridview does not contain the datetimepicker control. Therefore, it is more difficult to implement columns with input dates. You can add the datetimepicker control to the datagridview by using the following methods.

First, add a datagridview control to the front-end design and name it datagridview1. (Text/piikee)

Then, write the following code to the background. CS file:

[CSHARP]
View plaincopyprint?
  1. Using system;
  2. Using system. Collections. Generic;
  3. Using system. componentmodel;
  4. Using system. Data;
  5. Using system. drawing;
  6. Using system. LINQ;
  7. Using system. text;
  8. Using system. Windows. forms;
  9. Namespace moonlight_treasure
  10. {
  11. Public partial class mycount: Form
  12. {
  13. Datetimepicker DTP = new datetimepicker (); // A datetimepicker control is instantiated here.
  14. Rectangle _ rectangle;
  15. Public mycount ()
  16. {
  17. Initializecomponent ();
  18. Datagridview1.controls. Add (DTP); // Add the time control to the datagridview
  19. DTP. Visible = false; // do not show it first
  20. DTP. format = datetimepickerformat. Custom; // you can specify the date format as 2010-08-05.
  21. DTP. textchanged + = new eventhandler (dtp_textchange); // Add the event dtp_textchange for the Time Control
  22. }
  23. ****************/
  24. Private void dtp_textchange (Object sender, eventargs E)
  25. {
  26. Datagridview1.currentcell. value = DTP. Text. tostring (); // when the time control is selected, the time is assigned to the corresponding cell.
  27. }
  28. /**************** The cell is clicked, determines whether the column of the time control is *******************/
  29. Private void maid cellclick (Object sender, maid E)
  30. {
  31. If (E. columnindex = 0)
  32. {
  33. _ Rectangle = maid (E. columnindex, E. rowindex, true); // obtain the location and size of the cell
  34. DTP. size = new size (_ rectangle. Width, _ rectangle. Height); // assign the cell size to the time control.
  35. DTP. Location = new point (_ rectangle. X, _ rectangle. Y); // assign the cell position to the time control.
  36. DTP. Visible = true; // controls can be displayed.
  37. }
  38. Else
  39. DTP. Visible = false;
  40. }
  41. /*********** When the column width changes, the time control is hidden first, otherwise, the control cannot grow as the cell grows ***********/
  42. Private void datagridview1_columnwidthchanged (Object sender, datagridviewcolumneventargs E)
  43. {
  44. DTP. Visible = false;
  45. }
  46. /*********** When the scroll bar is rolling, the cell position changes and the time control must be hidden, otherwise, the time control location will be messy ********/
  47. Private void datagridview1_scroll (Object sender, scrolleventargs E)
  48. {
  49. DTP. Visible = false;
  50. }
  51. }
  52. }

 

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.