EDN Admin
Well-known member
Hello,
Objective is to display a datagridview in VS2010 Windows Forms application containing two calendar columns.
Eventually user may enter a value or value should be null. There is no default date truly applicable. Data will come from a dataset obtained from database as data is saved.
Original code and numerous tries down the line still have a problem and cannot even display eight rows of dates in single calendar column.
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; Public <span style="color:Blue; Overrides <span style="color:Blue; Sub InitializeEditingControl(rowIndex <span style="color:Blue; As <span style="color:Blue; Integer, initialFormattedValue <span style="color:Blue; As <span style="color:Blue; Object, dataGridViewCellStyle <span style="color:Blue; As DataGridViewCellStyle)
<span style="color:Green; Set the value of the editing control to the current cell value.
<span style="color:Blue; MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)
<span style="color:Blue; Dim ctl <span style="color:Blue; As CalendarEditingControl = TryCast(DataGridView.EditingControl, CalendarEditingControl)
<span style="color:Blue; Dim val <span style="color:Blue; As <span style="color:Blue; Object = <span style="color:Blue; Nothing
<span style="color:Blue; Try
val = <span style="color:Blue; Me.Value
<span style="color:Blue; Catch ex <span style="color:Blue; As Exception
<span style="color:Green; Argument ot of range (value doesnt exist in collection)
<span style="color:Blue; Return
<span style="color:Blue; End <span style="color:Blue; Try
<span style="color:Blue; If <span style="color:Blue; Not IsDBNull(val) <span style="color:Blue; Then
ctl.Value = <span style="color:Blue; DirectCast(val, DateTime)
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; Sub
[/code]
Error on line ctl.value = DirectCast(val,DateTime)
Message is InvalidCastException was unhandled by user code (Specified cast is not valid)
Also tried:
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; Public <span style="color:Blue; Overrides <span style="color:Blue; Sub InitializeEditingControl(<span style="color:Blue; ByVal rowIndex <span style="color:Blue; As <span style="color:Blue; Integer, _
<span style="color:Blue; ByVal initialFormattedValue <span style="color:Blue; As <span style="color:Blue; Object, _
<span style="color:Blue; ByVal dataGridViewCellStyle <span style="color:Blue; As DataGridViewCellStyle)
<span style="color:Green; Set the value of the editing control to the current cell value.
<span style="color:Blue; MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, _
dataGridViewCellStyle)
<span style="color:Blue; Dim ctl <span style="color:Blue; As CalendarEditingControl = _
<span style="color:Blue; CType(DataGridView.EditingControl, CalendarEditingControl)
<span style="color:Green; Use the default row value when Value property is null.
<span style="color:Blue; If (<span style="color:Blue; Me.Value <span style="color:Blue; Is <span style="color:Blue; Nothing) <span style="color:Blue; Then
ctl.Value = <span style="color:Blue; CType(<span style="color:Blue; Me.DefaultNewRowValue, DateTime)
<span style="color:Blue; Else
ctl.Value = <span style="color:Blue; CType(<span style="color:Blue; Me.Value, DateTime)
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; Sub
[/code]
<br/>
Problem is this line: ctl.Value = CType(Me.Value, DateTime)<br/>
and message is InvalidCastException was unhandled by usercode
Conversion from type DataRow to type Date is not valid.
Technique for loading grid is as follows:
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; Dim lStartDtCol <span style="color:Blue; As <span style="color:Blue; New CalendarColumn
<span style="color:Blue; Dim lRow <span style="color:Blue; As DataRow
<span style="color:Blue; Dim lFirstRow <span style="color:Blue; As <span style="color:Blue; Boolean = <span style="color:Blue; True
<span style="color:Blue; Dim lFirstColumn <span style="color:Blue; As <span style="color:Blue; Boolean = <span style="color:Blue; True
<span style="color:Blue; Dim i <span style="color:Blue; As Int16
<span style="color:Blue; Dim lRowID <span style="color:Blue; As Int16
<span style="color:Blue; Dim lColID <span style="color:Blue; As Int16
<span style="color:Blue; With grdDataEntry
.AllowUserToAddRows = <span style="color:Blue; True
.AllowUserToDeleteRows = <span style="color:Blue; True
.AutoGenerateColumns = <span style="color:Blue; False
lStartDtCol.Name = <span style="color:#A31515; "start_dt"
lStartDtCol.HeaderText = <span style="color:#A31515; "Start Date"
.Columns.Add(lStartDtCol)
<span style="color:Blue; For <span style="color:Blue; Each lRow <span style="color:Blue; In pData.Tables(0).Rows
<span style="color:Blue; If lFirstRow <span style="color:Blue; Then
lRowID = 0
lFirstRow = <span style="color:Blue; False
<span style="color:Blue; Else
lRowID += 1
<span style="color:Blue; End <span style="color:Blue; If
lFirstColumn = <span style="color:Blue; True
<span style="color:Blue; For <span style="color:Blue; Each lColumn <span style="color:Blue; In pData.Tables(0).Columns
<span style="color:Blue; If lFirstColumn <span style="color:Blue; Then
lColID = 0
lFirstColumn = <span style="color:Blue; False
<span style="color:Blue; Else
lColID += 1
<span style="color:Blue; End <span style="color:Blue; If
.Rows(lRowID).Cells(lColID).Value = lRow.Item(lColID)
<span style="color:Blue; Next
.Rows.Add(lRow)
<span style="color:Blue; Next
[/code]
Have spent hours and hours trying to solve - datagridviews are new to me and this may be something quite obvious, but it escapes me.
Any pointers would be wonderful
View the full article
Objective is to display a datagridview in VS2010 Windows Forms application containing two calendar columns.
Eventually user may enter a value or value should be null. There is no default date truly applicable. Data will come from a dataset obtained from database as data is saved.
Original code and numerous tries down the line still have a problem and cannot even display eight rows of dates in single calendar column.
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; Public <span style="color:Blue; Overrides <span style="color:Blue; Sub InitializeEditingControl(rowIndex <span style="color:Blue; As <span style="color:Blue; Integer, initialFormattedValue <span style="color:Blue; As <span style="color:Blue; Object, dataGridViewCellStyle <span style="color:Blue; As DataGridViewCellStyle)
<span style="color:Green; Set the value of the editing control to the current cell value.
<span style="color:Blue; MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)
<span style="color:Blue; Dim ctl <span style="color:Blue; As CalendarEditingControl = TryCast(DataGridView.EditingControl, CalendarEditingControl)
<span style="color:Blue; Dim val <span style="color:Blue; As <span style="color:Blue; Object = <span style="color:Blue; Nothing
<span style="color:Blue; Try
val = <span style="color:Blue; Me.Value
<span style="color:Blue; Catch ex <span style="color:Blue; As Exception
<span style="color:Green; Argument ot of range (value doesnt exist in collection)
<span style="color:Blue; Return
<span style="color:Blue; End <span style="color:Blue; Try
<span style="color:Blue; If <span style="color:Blue; Not IsDBNull(val) <span style="color:Blue; Then
ctl.Value = <span style="color:Blue; DirectCast(val, DateTime)
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; Sub
[/code]
Error on line ctl.value = DirectCast(val,DateTime)
Message is InvalidCastException was unhandled by user code (Specified cast is not valid)
Also tried:
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; Public <span style="color:Blue; Overrides <span style="color:Blue; Sub InitializeEditingControl(<span style="color:Blue; ByVal rowIndex <span style="color:Blue; As <span style="color:Blue; Integer, _
<span style="color:Blue; ByVal initialFormattedValue <span style="color:Blue; As <span style="color:Blue; Object, _
<span style="color:Blue; ByVal dataGridViewCellStyle <span style="color:Blue; As DataGridViewCellStyle)
<span style="color:Green; Set the value of the editing control to the current cell value.
<span style="color:Blue; MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, _
dataGridViewCellStyle)
<span style="color:Blue; Dim ctl <span style="color:Blue; As CalendarEditingControl = _
<span style="color:Blue; CType(DataGridView.EditingControl, CalendarEditingControl)
<span style="color:Green; Use the default row value when Value property is null.
<span style="color:Blue; If (<span style="color:Blue; Me.Value <span style="color:Blue; Is <span style="color:Blue; Nothing) <span style="color:Blue; Then
ctl.Value = <span style="color:Blue; CType(<span style="color:Blue; Me.DefaultNewRowValue, DateTime)
<span style="color:Blue; Else
ctl.Value = <span style="color:Blue; CType(<span style="color:Blue; Me.Value, DateTime)
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; Sub
[/code]
<br/>
Problem is this line: ctl.Value = CType(Me.Value, DateTime)<br/>
and message is InvalidCastException was unhandled by usercode
Conversion from type DataRow to type Date is not valid.
Technique for loading grid is as follows:
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; Dim lStartDtCol <span style="color:Blue; As <span style="color:Blue; New CalendarColumn
<span style="color:Blue; Dim lRow <span style="color:Blue; As DataRow
<span style="color:Blue; Dim lFirstRow <span style="color:Blue; As <span style="color:Blue; Boolean = <span style="color:Blue; True
<span style="color:Blue; Dim lFirstColumn <span style="color:Blue; As <span style="color:Blue; Boolean = <span style="color:Blue; True
<span style="color:Blue; Dim i <span style="color:Blue; As Int16
<span style="color:Blue; Dim lRowID <span style="color:Blue; As Int16
<span style="color:Blue; Dim lColID <span style="color:Blue; As Int16
<span style="color:Blue; With grdDataEntry
.AllowUserToAddRows = <span style="color:Blue; True
.AllowUserToDeleteRows = <span style="color:Blue; True
.AutoGenerateColumns = <span style="color:Blue; False
lStartDtCol.Name = <span style="color:#A31515; "start_dt"
lStartDtCol.HeaderText = <span style="color:#A31515; "Start Date"
.Columns.Add(lStartDtCol)
<span style="color:Blue; For <span style="color:Blue; Each lRow <span style="color:Blue; In pData.Tables(0).Rows
<span style="color:Blue; If lFirstRow <span style="color:Blue; Then
lRowID = 0
lFirstRow = <span style="color:Blue; False
<span style="color:Blue; Else
lRowID += 1
<span style="color:Blue; End <span style="color:Blue; If
lFirstColumn = <span style="color:Blue; True
<span style="color:Blue; For <span style="color:Blue; Each lColumn <span style="color:Blue; In pData.Tables(0).Columns
<span style="color:Blue; If lFirstColumn <span style="color:Blue; Then
lColID = 0
lFirstColumn = <span style="color:Blue; False
<span style="color:Blue; Else
lColID += 1
<span style="color:Blue; End <span style="color:Blue; If
.Rows(lRowID).Cells(lColID).Value = lRow.Item(lColID)
<span style="color:Blue; Next
.Rows.Add(lRow)
<span style="color:Blue; Next
[/code]
Have spent hours and hours trying to solve - datagridviews are new to me and this may be something quite obvious, but it escapes me.
Any pointers would be wonderful
View the full article