To use ComboBox, DateTimePicker in DataGrid, I can rewrite System.Windows.Forms.DataGridColumnStyle to do ... (C # section Click)
First, bind ComboBox with DataGridColumnStyle, then rewrite DataGridColumnStyle ... Code and Under:
Imports system
Imports system.drawing
Imports System.Windows.Forms
Public Class X_DataGridComboBoxStyle
Inherits System.Windows.Forms.DataGridColumnStyle
'' 'Defines a Combox, then rewrite the Combox to DataGridColumnStyle ...
Private x_comboBOX as ComboBox = New ComboBox
Private iSediting as boolean
Sub new ()
Me.x_comboBox.visible = false
End Sub
Protected Overrides Sub Abort (Byval Rownum As Integer)
Me.Iditing = FALSE
Me.invalidate ()
End Sub
Protected Overrides Function Commit (Byval DataSource As System.Windows.Forms.currencyManager, BYVAL ROWNUM AS INTEGER) AS BOOLEAN
ME.X_COMBOBOX.BOUNDS = Rectangle.empty
AddHandler X_comboBox.click, Addressof ComboBoxValueChanged '' Activation Event
IF not me.isediting then
Return True
END IF
ISEDITING = FALSE
Try
DIM Value As String
Value = x_comboBox.text
SetColumnValueatrow (Datasource, Rownum, Value)
Catch exception
Abort (Rownum)
Return False
END TRY
Invalidate ()
Return True
END FUNCTION
Protected Overloads Overrides Sub Edit (ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal bounds As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, ByVal instantText As String, ByVal cellIsVisible As Boolean)
DIM Value As String
Value = Convert.TOString (GetColumnValueatrow (Source, Rownum))
IF Cellisvisible Then
Me.x_comboBox.bounds = New Rectangle (Bounds.x 2, Bounds.Y 2, Bounds.width - 4, Bounds.Height - 4)
Me.x_comboBOX.Text = Value
ME.X_COMBOBOX.VISIBLE = true
AddHandler X_comboBox.click, Addressof ComboBoxValueChanged '' Activation Event Else
Me.x_comboBOX.Text = Value
Me.x_comboBox.visible = false
END IF
IF me.x_comboBox.visible the
DataGridtablestyle.DataGrid.invalidate (Bounds)
END IF
End Sub
Protected Overrides Function GETMINIMUMHEIGHT () AS INTEGER
Return 24
END FUNCTION
Protected Overrides function getpreferredheight (Byval g as system.drawing.graphics, byval value as object) AS integer
Return 24
END FUNCTION
Protected Overrides function getpreferredsize (Byval g as system.drawing.graphics, byval value as object) as system.drawing.size
Return New Size (100, 24)
END FUNCTION
Protected Overloads Overrides Sub Paint (ByVal g As System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer)
Paint (G, Bounds, Source, Rownum, False)
End Sub
Protected Overloads Overrides Sub Paint (ByVal g As System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal alignToRight As Boolean)
Paint (G, Bounds, Source, Rownum, Brushes.Red, Brushes.blue, Aligntoright)
End Sub
Protected Overloads Overrides Sub Paint (ByVal g As System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal foreBrush As Brush, ByVal backBrush As Brush, Byval aligntoright as boolean)
DIM DATES AS STRING = Convert.TOString (GetColumnValueatrow (Source, Rownum))
Dim Rect as system.drawing.Rectangle = Bounds
g.FillRectangle (Brushes.White, Rect)
Rect.offset (0, 2)
RECT.HEIGHT - = 2
g.drawstring (dates, me.datagridtable.datagrid.font, brushes.black, rest, rest.y) End Sub
Protected Overloads overrides sub setDataGridincolumn (Byval Value As DataGrid)
Mybase.setdataGridincolumn (Value)
IF not me.x_comboBox.parent is nothing then
Me.x_comboBox.visible = false
Me.x_comboBox.controls.remove (Me.x_comboBox)
END IF
IF not value is nothing then
Value.controls.add (me.x_comboBox)
END IF
End Sub
'' 'Defines a Comboxchange event
Private Sub ComboBoxValueChanged (Byval EVENTARGS)
Me.Iditing = True
Mybase.columnstartedededIting (x_comboBox)
End Sub
'' 'Defines an Items added to add Combox to add attributes
Public Sub AddItem (Byval stritemname as string)
Me.x_comboBox.Items.add (stritemname)
End Sub
END CLASS
Second, bind DateTimePicker with DataGridColumnStyle, then rewrite DataGridColumnStyle ... Code and under:
Imports system
Imports system.drawing
Imports System.Windows.Forms
Public Class X_DataGridDateTimePickerColumnStyle
Inherits System.Windows.Forms.DataGridColumnStyle
Private iSediting as boolean
'' 'Defines a DateTimePicker
Private x_datetimepicker as system.windows.forms.datetimepicker = new datetimepicker
Sub new ()
Me.x_datetimepicker.visible = false
End Sub
Protected Overrides Sub Abort (Byval Rownum As Integer)
Me.Iditing = FALSE
Me.invalidate ()
End Sub
Protected Overrides Function Commit (Byval DataSource As System.Windows.Forms.currencyManager, BYVAL ROWNUM AS INTEGER) AS BOOLEAN
Me.x_datetimepicker.bounds = Rectangle.empty
AddHandler X_DatetimePicker.ValueChanged, Addressof Me.DatePickervilueChange '' 'activation event
IF not me.isediting then
Return True
END IF
Me.Iditing = FALSE
Try
Dim value as string = me.x_datetimepicker.valueSetColumnValueatrow (DataSource, Rownum, Value)
Catch exception
Me.abort (rownum)
Return False
END TRY
Me.invalidate ()
Return True
END FUNCTION
Protected Overloads Overrides Sub Edit (ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal bounds As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, ByVal instantText As String, ByVal cellIsVisible As Boolean)
DIM Value As String
Value = convert.todatetime (getColumnValueatrow (Source, Rownum))
IF Cellisvisible Then
Me.x_datetimepicker.bounds = new Rectangle (Bounds.x 2, Bounds.Y 2, Bounds.Width - 4, Bounds.Height - 4)
Me.x_datetimePicker.Value = Value
Me.x_datetimepicker.visible = true
AddHandler X_DatetimePicker.ValueChanged, Addressof Me.DatePickervilueChange '' 'activation event
Else
Me.x_datetimePicker.Value = Value
Me.x_datetimepicker.visible = false
END IF
If me.x_datetimepicker.visible the
DataGridtablestyle.DataGrid.invalidate (Bounds)
END IF
End Sub
Protected Overrides Function GETMINIMUMHEIGHT () AS INTEGER
Return 24
END FUNCTION
Protected Overrides function getpreferredheight (Byval g as system.drawing.graphics, byval value as object) AS integer
Return 24
END FUNCTION
Protected Overrides function getpreferredsize (Byval g as system.drawing.graphics, byval value as object) as system.drawing.size
Return New Size (100, 24)
END FUNCTION
Protected Overloads Overrides Sub Paint (ByVal g As System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer)
Paint (G, Bounds, Source, Rownum, False)
End Sub
Protected Overloads Overrides Sub Paint (ByVal g As System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal alignToRight As Boolean) Paint (g, bounds , Source, Rownum, Brushes.Red, Brushes.blue, Aligntoright
End Sub
Protected Overloads Overrides Sub Paint (ByVal g As System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal forebrush As Brush, ByVal bcakbrush As Brush, Byval aligntoright as boolean)
DIM DATES AS STRING = Convert.TodateTime (GetColumnValueatrow (Source, Rownum)
Dim Rect as system.drawing.Rectangle = Bounds
g.FillRectangle (Brushes.White, Rect)
Rect.offset (0, 2)
RECT.HEIGHT - = 2
g.drawstring (dates, me.datagridtable.DataGrid.font, brushes.black, rest, rest.y)
End Sub
'' 'Defines a DatePickervilueChange event for operation.
Private sub datepickerviluechange (byval e AS Object, Byval E As Eventargs)
Me.Iditing = True
Mybase.columnstartedededIting (x_datetimepicker)
End Sub
Protected Overloads overrides sub setDataGridincolumn (Byval Value As DataGrid)
Mybase.setdataGridincolumn (Value)
IF not me.x_datetimepicker.parent is nothing then
Me.x_datetimepicker.visible = false
Me.x_datetimepicker.controls.remove (me.x_datetimepicker)
END IF
IF not value is nothing then
Value.Controls.add (me.x_datetimepicker)
END IF
End Sub
End class ● At this point, both overwritten UiControl have been completed, now create a new form, place a DataGird, we will implement the use of ComboBox, DateTimePicker, Checkbox ....
Code section: public class form1
Inherits System.Windows.Forms.form
#Region "Windows Form Designer Generated Code" Public Sub New ()
Mybase.new ()
'This Call is Required by the Windows Form Designer.
InitializeComponent ()
'Add anyinitization after the initializecomponent () CALL
End Sub
'Form Overrides Dispose to Clean Up The Component List.
Protected Overloads Overrides Sub Dispose (Byval Disposing as Boolean)
IF Disposing then
IF not (Components Is Nothing) THEN
Components.dispose ()
END IF
END IF
Mybase.dispose (Disposing)
End Sub
'Required by the Windows Form Designer
Private Components as System.comPonentModel.icontainer
'NOTE: The Following Procedure Is Required by The Windows Form Designer
'It can be modified using the windows form designer.
'Do Not Modify It Using The Code Editor.
Friend Withevents DataGrid1 As System.Windows.Forms.DataGrid
Me.DataGrid1 = new system.windows.forms.DataGrid
Ctype (me.datagrid1, system.componentmodel.isupportinitialize) .beginInit ()
Me.suspendlayout ()
'
'DataGrid1
'
Me.DataGrid1.anchor = ctype (((System.Windows.Forms.Anchorstyles.top or system.windows.Forms.Anchorstyles.bottom) _
OR system.windows.forms.Anchorstyles.left)
Or System.Windows.Forms.Anchorstyles.right, System.Windows.Forms.Anchorstyles
Me.DataGrid1.datamember = ""
Me.DataGrid1.HeaderforeColor = system.drawing.systemcolors.ControlText
Me.DataGrid1.location = new system.drawing.point (4, 4)
Me.DataGrid1.name = "DataGrid1"
Me.DataGrid1.size = new system.drawing.size (634, 246)
Me.DataGrid1.tabindex = 0
'
'Form1
'
Me.autoscalebasesize = new system.drawing.size (5, 13) me.clientsize = new system.drawing.size (642, 253)
Me.Controls.add (me.datagrid1)
Me.Name = "Form1"
Me.Text = "Form1"
Ctype (me.datagrid1, system.componentmodel.isupportinitialize) .endinit ()
Me.ResumeLayout (false)
End Sub
#End region
Private IDTB_TEMP AS New DataTable
Private sub adddate ()
DIM IDRW_ROW AS DATAROW
IDRW_ROW = IDTB_TEMP.NEWROW
IDRW_ROW.ITEM ("name") = "xx0"
IDRW_ROW.Item ("state") = "111"
Idrw_row.item ("dates") = system.datetime.now.toshortdateString
IDRW_ROW.Item ("Chioce") = TRUE
IDTB_TEMP.ROWS.ADD (iDrw_row)
IDRW_ROW = IDTB_TEMP.NEWROW
IDRW_ROW.ITEM ("name") = "xx0"
IDRW_ROW.ITEM ("State") = "222"
Idrw_row.item ("dates") = system.datetime.now.toshortdateString
IDRW_ROW.Item ("Chioce") = false
IDTB_TEMP.ROWS.ADD (iDrw_row)
End Sub
Private sub DataStyle ()
Dim MyGridStyle As DataGridtableStyle = New DataGridTableStyle
MyGridStyle.mappingName = "name"
Dim NameColumnStyle As DataGridTextBoxColumn = New DataGridTextBoxColumn
NameColumnStyle.mappingName = "name"
NameColumnStyle.Width = 100
NameColumnStyle.Headertext = "Name"
MyGridStyle.GridColumnStyles.Add (NameColumnStyle)
DIM ComboxcolumnStyle As X_DataGridcomboBoxStyle = New X_DataGridComboBoxStyle
ComboxcolumnStyle.mappingName = "state"
ComboxcolumnStyle.Headertext = "Status"
ComboxcolumnStyle.width = 100
ComboxcolumnStyle.Additem ("111")
ComboxColumnStyle.Additem ("222")
MyGridStyle.GridColumnStyles.Add (ComboxColumnStyle)
DIM DATEPICKCOLUMNSTYLE AS X_DATAGRIDDATETIMEPICKERCOLUMNSTY = New X_DataGriddateTimePickerColumnStyle
DatePickColumnStyle.mappingName = "dates"
DatePickColumnStyle.Headertext = "Date"
DatepickColumnStyle.Width = "100"
MyGridStyle.GridColumnStyles.Add (DatePickColumnStyle)
Dim BoolcolumnStyle As DataGridBoolcolumn = New DataGridBoolColumn
BoolcolumnStyle.mappingname = "chioc"
BoolcolumnStyle.Headertext = "Select"
BoolcolumnStyle.Width = 100
BoolcolumnStyle.allownull = false '' '
BoolcolumnStyle.trueValue = true '' 'Solving the trisopular problem of Checkbox
BoolcolumnStyle.falsevalue = false '' '
BoolcolumnStyle.Readonly = false '' '
MyGridStyle.GridColumnStyles.Add (BoolcolumnStyle)
Me.DataGrid1.tables.add (MyGridStyle)
End Sub
Private Sub Form1_Load (Byval E AS System.Object, Byval E AS System.Eventargs) Handles MyBase.Load
IDTB_TEMP = New DataTable ("NameTable")
IDTB_TEMP.COLUMNS.ADD (New Datacolumn ("Name"))
IDTB_TEMP.COLUMNS.ADD (New Datacolumn ("State"))
DIM DATECOLUMNS AS Datacolumn
DateColumns = New Datacolumn ("Dates", Type.gettype ("System.Datetime"))))
IDTB_TEMP.COLUMNS.ADD (DateColumn)
IDTB_TEMP.COLUMNS.ADD (New Datacolumn ("Chioce", Type.gettype ("System.Boolean"))))))))
Me.DataGrid1.datasource = IDTB_TEMP
Datastyle ()
Adddate ()
End Sub
END CLASS
Running renderings (1): Combox in data
Running renderings (2): DateTimePicker in DataGrid