Question : Assistance with Windows Forms and calendar control embedded inside a grid

Hi,
I am running VS2005 and developing a Windows From using VB.  I have
attempted to create a simple grid attached to a table adadpter which is
connected to a MSSQL 2005 database; and followed instructions to embed a
calendar control inside a cell as per:
http://msdn.microsoft.com/en-us/library/7tas5c80.aspx

When I initially load a filtered set of data, all is well and the calendar
control seemingly works perfectly (ie: the calendar controls work as
desired).  However, when I load a different subset of data and run across a
date in the format of "1/1/2008" I get an error message in the following code
snippet (code from MS website above):

=======================================
    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)
error here ---->        ctl.Value = CType(Me.Value, DateTime)

    End Sub
===============================================

How do I go about trapping this error and correcting the problem?  Here is
the error detail:

System.ArgumentOutOfRangeException was unhandled by user code
  Message="Specified argument was out of the range of valid values.
Parameter name: rowIndex"
  ParamName="rowIndex"
  Source="System.Windows.Forms"
  StackTrace:
       at System.Windows.Forms.DataGridViewCell.GetValue(Int32 rowIndex)
       at System.Windows.Forms.DataGridViewCell.get_Value()
       at Diversion_Data_Entry.CalendarCell.InitializeEditingControl(Int32
rowIndex, Object initialFormattedValue, DataGridViewCellStyle
dataGridViewCellStyle) in C:\Visual Studio 2005\Visual Source Safe
Projects\test\CalendarColumn.vb:line 48
       at
System.Windows.Forms.DataGridView.InitializeEditingControlValue(DataGridViewCellStyle& dataGridViewCellStyle, DataGridViewCell dataGridViewCell)
=====================================================

thank you
suz

Answer : Assistance with Windows Forms and calendar control embedded inside a grid


Hi

First make sure the attribute that contains the date "is formatted as date"

Modified this piece to also catch the dbnull exception ,(for an emty string beneath calender,(emty cell))

    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)
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        If (Me.Value.Equals(System.DBNull.Value)) Then
            ctl.Value = Now.ToShortDateString
        Else
            ctl.Value = CType(Me.Value, DateTime)
        End If
    End Sub

--------------------------------------------------------------------------------------------------------------

hope this helps

vbturbo
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
 
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 (value IsNot Nothing) AndAlso _
                Not value.GetType().IsAssignableFrom(GetType(CalendarCell)) _
                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.EditingControlFormattedValue
 
        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.GetEditingControlFormattedValue
 
        Return Me.Value.ToShortDateString()
 
    End Function
 
    Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As _
        DataGridViewCellStyle) _
        Implements IDataGridViewEditingControl.ApplyCellStyleToEditingControl
 
        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.EditingControlWantsInputKey
 
        ' 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.PrepareEditingControlForEdit
 
        ' No preparation needs to be done.
 
    End Sub
 
    Public ReadOnly Property RepositionEditingControlOnValueChange() _
        As Boolean Implements _
        IDataGridViewEditingControl.RepositionEditingControlOnValueChange
 
        Get
            Return False
        End Get
 
    End Property
 
    Public Property EditingControlDataGridView() As DataGridView _
        Implements IDataGridViewEditingControl.EditingControlDataGridView
 
        Get
            Return dataGridViewControl
        End Get
        Set(ByVal value As DataGridView)
            dataGridViewControl = value
        End Set
 
    End Property
 
    Public Property EditingControlValueChanged() As Boolean _
        Implements IDataGridViewEditingControl.EditingControlValueChanged
 
        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.NotifyCurrentCellDirty(True)
        MyBase.OnValueChanged(eventargs)
 
    End Sub
 
End Class
Random Solutions  
 
programming4us programming4us