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 ();
}
}