Using system;
Using system.data;
Using system.windows.forms;
Using system.drawing;
// this Example Shows How To create your owmbs
// Hosts a Control, in this case, a datetimepicker.
Public Class DataGridtimePickerColumn: DataGridColumnStyle
{
Private richtextbox mydatetimepicker = new richtextbox ();
// the itditing field tracks WHETHER or NOT TRACKS WHETHER or NOT THE USER IS
// Editing Data with the Hosted Control.
Private bool iSEDITING;
Public DataGridTimePickerColumn (): base ()
{
MyDatetimePicker.visible = false;
}
Protected Override Void Abort (int Rownum)
{
iSEDITING = FALSE;
Invalidate ();
}
Protected Override Bool Compit
CurrencyManager DataSource, int Rownum
{
MyDatetimePicker.bounds = Rectangle.empty;
IF (! iSediting)
Return True;
iSEDITING = FALSE;
Try
{
DateTime value = system.datetime.parse (MyDateTimePicker.Text);
SetColumnValueatrow (DataSource, Rownum, Value);
}
Catch (Exception)
{
Abort (rownum);
Return False;
}
Invalidate ();
Return True;
}
Protected Override Void Edit
CurrencyManager Source,
Int rownum,
Rectangle Bounds,
Bool Readonly,
String InstantText,
Bool Cellisvisible)
{
DateTime Value = (DateTime)
GetColumnValueatrow (Source, Rownum);
IF (CellisVisible)
{
MyDatetimePicker.Bounds = New Rectangle
(Bounds.x 2, Bounds.y 2,
Bounds.width - 4, Bounds.height - 4);
MyDatetimePicker.text = value.toString ();
mydatetimepicker.visible = true;
}
Else
{
MyDatetimePicker.text = value.toString ();
MyDatetimePicker.visible = false;
}
IF (MyDateTimePicker.visible)
DataGridtablesTyle.DataGrid.invalidate (Bounds);
}
Protected Override Size getPreferRedsize
Graphics g,
Object value
{
Return new size (100, mydatetimepicker.preferredhem 4);}
Protected Override Int getminimumHeight ()
{
Return mydatetimepicker.preferredheight 4;
}
Protected Override Int GetPreferRedheight (Graphics G,
Object value
{
Return mydatetimepicker.preferredheight 4;
}
Protected Override Void Paint (Graphics G,
Rectangle Bounds,
CurrencyManager Source,
Int rownum)
{
Paint (g, bounds, source, rownum, false);
}
Protected Override Void Paint
Graphics g,
Rectangle Bounds,
CurrencyManager Source,
Int rownum,
Bool aligntoright)
{
Paint
g, bounds,
Source,
Rownum,
Brushes.red,
Brushes.blue,
Aligntoright);
}
Protected Override Void Paint
Graphics g,
Rectangle Bounds,
CurrencyManager Source,
Int rownum,
Brush backbrush,
Brush forebrush,
Bool aligntoright)
{
DateTime Date = (DateTime)
GetColumnValueatrow (Source, Rownum);
Rectangle Rect = Bounds;
g.fillRectangle (Backbrush, Rect);
Rect.offset (0, 2);
Rect.height - = 2;
g.drawstring (Date.ToString ("D"),
This.dataGridtablestyle.DataGrid.font,
Forebrush, Rect);
}
Protected Override Void SetDataGridIncolumn (DataGrid Value)
{
Base.setDataGridincolumn (value);
IF (MyDateTimePicker.Parent! = null)
{
MyDateTimePicker.Parent.Controls.remove
mydatetimePicker;
}
IF (value! = null)
{
Value.controls.add (MyDateTimePicker);
}
}
Private Void TimePickerValueChanged (Object Sender, Eventargs E)
{
this.iseditation = true;
Base.columnStartedEditing (MyDateTimePicker);
}
}
Namespace DataGridColumnStyleexample
{
Using system;
Using system.data;
Using system.windows.forms;
Using system.drawing;
Using system.componentmodel;
Public Class MyForm: Form
{
Private DataTable namesDataTable;
Private DataGrid Grid = New DataGrid (); Public MyForm (): Base ()
{
INITFORM ();
NamesDataTable = New DataTable ("nameSTable");
NamesDataTable.columns.add (New Datacolumn ("Name"));
Datacolumn datecolumn = new datacolumn
("Date", TypeOf (datetime);
DateColumn.defaultValue = datetime.toDay;
NamesDataTable.columns.add (datecolumn);
DataSet nameDataSet = new dataset ();
NamesDataSet.tables.add (namesdataable);
Grid.datasource = namesdata
Grid.Datamember = "namestable";
AddgridStyle ();
Adddata ();
}
Private void addgridStyle ()
{
DataGridTableStyle MyGridStyle = New DataGridTableStyle ();
MyGridStyle.mappingName = "namestable";
DataGridTextBoxColumn NameColumnStyle =
New DataGridTextBoxColumn ();
NameColumnStyle.mappingname = "name";
NamecolumnStyle.Headertext = "?? 3?"
MyGridStyle.GridColumnStyles.Add (NameColumnStyle);
DataGridtimePickerColumn TimePickerColumnStyle =
New DataGridTimePickerColumn ();
TimePickerColumnStyle.mappingname = "Date";
TimePickerColumnStyle.Headertext = "Date";
TimePickerColumnStyle.Width = 100;
MyGridStyle.GridColumnStyles.Add (TimePickerColumnStyle);
Grid.tables.add (myGridStyle);
}
Private void adddata ()
{
DataRow DROW = NamesDataTable.NewRow ();
DROW ["name"] = "name 1";
DROW ["Date"] = New DateTime (2001, 12, 01);
NamesDataTable.Rows.Add (DROW);
DROW = NamesDataTable.newrow ();
DROW ["name"] = "name 2";
DROW ["Date"] = New DateTime (2001, 12, 04);
NamesDataTable.Rows.Add (DROW);
DROW = NamesDataTable.newrow ();
DROW ["name"] = "name 3";
DROW ["Date"] = New DateTime (2001, 12, 29); NamesDataTable.Rows.Add (DROW);
DROW = NamesDataTable.newrow ();
DROW ["name"] = "name 4";
DROW ["Date"] = New DateTime (2001, 12, 13);
NamesDataTable.Rows.Add (DROW);
DROW = NamesDataTable.newrow ();
DROW ["name"] = "name 5";
DROW ["Date"] = New DateTime (2001, 12, 21);
NamesDataTable.Rows.Add (DROW);
NamesDataTable.acceptchanges ();
}
Private void initform ()
{
this.size = new size (500, 500);
Grid.size = new size (350, 250);
Grid.tabstop = true;
Grid.tabindex = 1;
THIS.StartPosition = formstartPosition.centerscreen
This.Controls.Add (grid);
}
[Stathread]
Public static void
Main
()
{
MyForm myform1 = new myform ();
myform1.showdialog ();
}
}
}