Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 2005 > Pro Visual Basic 2005
|
Pro Visual Basic 2005 For advanced Visual Basic coders working in version 2005. Beginning-level questions will be redirected to other forums, including Beginning VB 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro Visual Basic 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 27th, 2006, 03:04 AM
Authorized User
 
Join Date: Oct 2004
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Default adding my own combo box in datagridview

heres my code downloaded in microsfot site
datetimepicker into datagridview

but what if i want to replce this into my own combobox?





Imports System

Imports System.Windows.Forms

Public Class CalendarColumn

Inherits DataGridViewColumn

Public Sub New()

MyBase.New(New CalendarCell())

End Sub

Public Overrides Property CellTemplate() As DataGridViewCell

Get

Return MyBase.CellTemplate

End Get

Set(ByVal value As DataGridViewCell)

' Ensure that the cell used for the template is a CalendarCell.

If Not (value Is Nothing) AndAlso _

Not value.GetType().IsAssignableFrom(GetType(CalendarC ell)) _

Then

Throw New InvalidCastException("Must be a CalendarCell")

End If

MyBase.CellTemplate = value

End Set

End Property

End Class

Public Class CalendarCell

Inherits DataGridViewTextBoxCell

Public Sub New()

' Use the short date format.

Me.Style.Format = "d"

End Sub

Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, _

ByVal initialFormattedValue As Object, _

ByVal dataGridViewCellStyle As DataGridViewCellStyle)

' Set the value of the editing control to the current cell value.

MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, _

dataGridViewCellStyle)

Dim ctl As CalendarEditingControl = _

CType(DataGridView.EditingControl, CalendarEditingControl)

ctl.Value = CType(Me.Value, DateTime)

End Sub

Public Overrides ReadOnly Property EditType() As Type

Get

' Return the type of the editing contol that CalendarCell uses.

Return GetType(CalendarEditingControl)

End Get

End Property

Public Overrides ReadOnly Property ValueType() As Type

Get

' Return the type of the value that CalendarCell contains.

Return GetType(DateTime)

End Get

End Property

Public Overrides ReadOnly Property DefaultNewRowValue() As Object

Get

' Use the current date and time as the default value.

Return DateTime.Now

End Get

End Property

End Class

Class CalendarEditingControl

Inherits DateTimePicker

Implements IDataGridViewEditingControl

Private dataGridViewControl As DataGridView

Private valueIsChanged As Boolean = False

Private rowIndexNum As Integer

Public Sub New()

Me.Format = DateTimePickerFormat.Short

End Sub

Public Property EditingControlFormattedValue() As Object _

Implements IDataGridViewEditingControl.EditingControlFormatte dValue

Get

Return Me.Value.ToShortDateString()

End Get

Set(ByVal value As Object)

If TypeOf value Is [String] Then

Me.Value = DateTime.Parse(CStr(value))

End If

End Set

End Property

Public Function GetEditingControlFormattedValue(ByVal context _

As DataGridViewDataErrorContexts) As Object _

Implements IDataGridViewEditingControl.GetEditingControlForma ttedValue

Return Me.Value.ToShortDateString()

End Function

Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As _

DataGridViewCellStyle) _

Implements IDataGridViewEditingControl.ApplyCellStyleToEditin gControl

Me.Font = dataGridViewCellStyle.Font

Me.CalendarForeColor = dataGridViewCellStyle.ForeColor

Me.CalendarMonthBackground = dataGridViewCellStyle.BackColor

End Sub

Public Property EditingControlRowIndex() As Integer _

Implements IDataGridViewEditingControl.EditingControlRowIndex

Get

Return rowIndexNum

End Get

Set(ByVal value As Integer)

rowIndexNum = value

End Set

End Property

Public Function EditingControlWantsInputKey(ByVal key As Keys, _

ByVal dataGridViewWantsInputKey As Boolean) As Boolean _

Implements IDataGridViewEditingControl.EditingControlWantsInp utKey

' Let the DateTimePicker handle the keys listed.

Select Case key And Keys.KeyCode

Case Keys.Left, Keys.Up, Keys.Down, Keys.Right, _

Keys.Home, Keys.End, Keys.PageDown, Keys.PageUp

Return True

Case Else

Return False

End Select

End Function

Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) _

Implements IDataGridViewEditingControl.PrepareEditingControlF orEdit

' No preparation needs to be done.

End Sub

Public ReadOnly Property RepositionEditingControlOnValueChange() _

As Boolean Implements _

IDataGridViewEditingControl.RepositionEditingContr olOnValueChange

Get

Return False

End Get

End Property

Public Property EditingControlDataGridView() As DataGridView _

Implements IDataGridViewEditingControl.EditingControlDataGrid View

Get

Return dataGridViewControl

End Get

Set(ByVal value As DataGridView)

dataGridViewControl = value

End Set

End Property

Public Property EditingControlValueChanged() As Boolean _

Implements IDataGridViewEditingControl.EditingControlValueCha nged

Get

Return valueIsChanged

End Get

Set(ByVal value As Boolean)

valueIsChanged = value

End Set

End Property

Public ReadOnly Property EditingControlCursor() As Cursor _

Implements IDataGridViewEditingControl.EditingPanelCursor

Get

Return MyBase.Cursor

End Get

End Property

Protected Overrides Sub OnValueChanged(ByVal eventargs As EventArgs)

' Notify the DataGridView that the contents of the cell have changed.

valueIsChanged = True

Me.EditingControlDataGridView.NotifyCurrentCellDir ty(True)

MyBase.OnValueChanged(eventargs)

End Sub

End Class

Public Class Form1

Inherits Form

Private dataGridView1 As New DataGridView()

<STAThreadAttribute()> _

Public Shared Sub Main()

Application.Run(New Form1())

End Sub

Public Sub New()

Me.dataGridView1.Dock = DockStyle.Fill

Me.Controls.Add(Me.dataGridView1)

Me.Text = "DataGridView calendar column demo"

End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) _

Handles Me.Load

Dim col As New CalendarColumn()

Me.dataGridView1.Columns.Add(col)

Me.dataGridView1.RowCount = 5

Dim row As DataGridViewRow

For Each row In Me.dataGridView1.Rows

row.Cells(0).Value = DateTime.Now

Next row

End Sub

End Class






heres my revised code i replaced datetimepicker into combo box

the problem now is how do i add items into combo box

later when this is solve..i will replace this combo box with my own multi column combo box

just copy and paste

hoping somebody can help me... :)





Imports System

Imports System.Windows.Forms

Public Class CalendarColumn

Inherits DataGridViewColumn

Public Sub New()

MyBase.New(New CalendarCell())

End Sub

Public Overrides Property CellTemplate() As DataGridViewCell

Get

Return MyBase.CellTemplate

End Get

Set(ByVal value As DataGridViewCell)

' Ensure that the cell used for the template is a CalendarCell.

If Not (value Is Nothing) AndAlso _

Not value.GetType().IsAssignableFrom(GetType(CalendarC ell)) _

Then

Throw New InvalidCastException("Must be a CalendarCell")

End If

MyBase.CellTemplate = value

End Set

End Property

End Class

Public Class CalendarCell

Inherits DataGridViewTextBoxCell

Public Sub New()

' Use the short date format.

'//// Me.Style.Format = "d"

End Sub

Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, _

ByVal initialFormattedValue As Object, _

ByVal dataGridViewCellStyle As DataGridViewCellStyle)

' Set the value of the editing control to the current cell value.

MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, _

dataGridViewCellStyle)

Dim ctl As CalendarEditingControl = _

CType(DataGridView.EditingControl, CalendarEditingControl)

'// ctl.Value = CType(Me.Value, DateTime)

ctl.Text = CType(Me.Value, String)

End Sub

Public Overrides ReadOnly Property EditType() As Type

Get

' Return the type of the editing contol that CalendarCell uses.

Return GetType(CalendarEditingControl)

End Get

End Property

Public Overrides ReadOnly Property ValueType() As Type

Get

' Return the type of the value that CalendarCell contains.

Return GetType(DateTime)

End Get

End Property

Public Overrides ReadOnly Property DefaultNewRowValue() As Object

Get

' Use the current date and time as the default value.

'//// Return DateTime.Now

End Get

End Property

End Class

Class CalendarEditingControl

'/// Inherits DateTimePicker

Inherits ComboBox

Implements IDataGridViewEditingControl

Private dataGridViewControl As DataGridView

Private valueIsChanged As Boolean = False

Private rowIndexNum As Integer

Public Sub New()

' Me.Format = DateTimePickerFormat.Short

End Sub

Public Property EditingControlFormattedValue() As Object _

Implements IDataGridViewEditingControl.EditingControlFormatte dValue

Get

'///// Return Me.Value.ToShortDateString()

Return Me.ValueMember

End Get

Set(ByVal value As Object)

If TypeOf value Is [String] Then

' ///Me.Value = DateTime.Parse(CStr(value))

End If

End Set

End Property

Public Function GetEditingControlFormattedValue(ByVal context _

As DataGridViewDataErrorContexts) As Object _

Implements IDataGridViewEditingControl.GetEditingControlForma ttedValue

'/// Return Me.Value.ToShortDateString()

Return Me.ValueMember

End Function

Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As _

DataGridViewCellStyle) _

Implements IDataGridViewEditingControl.ApplyCellStyleToEditin gControl

'//Me.Font = dataGridViewCellStyle.Font

'// Me.CalendarForeColor = dataGridViewCellStyle.ForeColor

'/// Me.CalendarMonthBackground = dataGridViewCellStyle.BackColor

End Sub

Public Property EditingControlRowIndex() As Integer _

Implements IDataGridViewEditingControl.EditingControlRowIndex

Get

Return rowIndexNum

End Get

Set(ByVal value As Integer)

rowIndexNum = value

End Set

End Property

Public Function EditingControlWantsInputKey(ByVal key As Keys, _

ByVal dataGridViewWantsInputKey As Boolean) As Boolean _

Implements IDataGridViewEditingControl.EditingControlWantsInp utKey

' Let the DateTimePicker handle the keys listed.

Select Case key And Keys.KeyCode

Case Keys.Left, Keys.Up, Keys.Down, Keys.Right, _

Keys.Home, Keys.End, Keys.PageDown, Keys.PageUp

Return True

Case Else

Return False

End Select

End Function

Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) _

Implements IDataGridViewEditingControl.PrepareEditingControlF orEdit

' No preparation needs to be done.

End Sub

Public ReadOnly Property RepositionEditingControlOnValueChange() _

As Boolean Implements _

IDataGridViewEditingControl.RepositionEditingContr olOnValueChange

Get

Return False

End Get

End Property

Public Property EditingControlDataGridView() As DataGridView _

Implements IDataGridViewEditingControl.EditingControlDataGrid View

Get

Return dataGridViewControl

End Get

Set(ByVal value As DataGridView)

dataGridViewControl = value

End Set

End Property

Public Property EditingControlValueChanged() As Boolean _

Implements IDataGridViewEditingControl.EditingControlValueCha nged

Get

Return valueIsChanged

End Get

Set(ByVal value As Boolean)

valueIsChanged = value

End Set

End Property

Public ReadOnly Property EditingControlCursor() As Cursor _

Implements IDataGridViewEditingControl.EditingPanelCursor

Get

Return MyBase.Cursor

End Get

End Property

'replce this ..this is for datetimepicker only

'Protected Overrides Sub OnValueChanged(ByVal eventargs As EventArgs)

' ' Notify the DataGridView that the contents of the cell have changed.

' valueIsChanged = True

' Me.EditingControlDataGridView.NotifyCurrentCellDir ty(True)

' MyBase.OnValueChanged(eventargs)

'End Sub

Protected Overrides Sub OnValueMemberChanged(ByVal e As System.EventArgs)

Me.EditingControlDataGridView.NotifyCurrentCellDir ty(True)

MyBase.OnValueMemberChanged(e)

End Sub



End Class

Public Class Form1

Inherits Form

Private dataGridView1 As New DataGridView()

<STAThreadAttribute()> _

Public Shared Sub Main()

Application.Run(New Form1())

End Sub

Public Sub New()

Me.dataGridView1.Dock = DockStyle.Fill

Me.Controls.Add(Me.dataGridView1)

Me.Text = "DataGridView calendar column demo"

End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) _

Handles Me.Load

Dim col As New CalendarColumn()

Me.dataGridView1.Columns.Add(col)

' Me.dataGridView1.RowCount = 5





'Dim row As DataGridViewRow

'For Each row In Me.dataGridView1.Rows

' row.Cells(0).Value = DateTime.Now

'Next row





End Sub

End Class








Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding value pairs to a combo box tebert2458 C# 2 February 25th, 2009 06:10 AM
Adding data in a combo box from the database Gini Visual Studio 2008 0 June 20th, 2008 02:17 AM
Locking a Combo Box - Sync with DataGridView hollertrek General .NET 0 March 24th, 2008 09:59 AM
Filling a combo box w/in datagridview kscase Visual Basic 2005 Basics 1 July 12th, 2007 07:26 AM
Multi-column combo box in datagridview aad1 C# 0 June 5th, 2006 09:58 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.