Change date in calendar column to reflect date in combo box
-
4 เมษายน 2555 10:41
I have a calendar column which is generated on from load. Its value is assigned to todays date - for instance 04/04/2012
I also have a comboBox with a list of months.
Is there a way I can change the value of my calendar column to reflect the chosen month?
For example, if I select January from my comboBox then have the date as 01/01/2012.
ComboBox1.SelectedIndex = -1 col = New CalendarColumn Me.dataGridView1.Columns.Add(col) Me.dataGridView1.Name = "Trip Date" Me.dataGridView1.RowCount = 1 Dim row As DataGridViewRow For Each row In Me.dataGridView1.Rows row.Cells(0).Value = DateTime.Now Next row 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) ' Use the default row value when Value property is null. If (Me.Value Is Nothing) Then ctl.Value = CType(Me.DefaultNewRowValue, DateTime) Else ctl.Value = CType(Me.Value, DateTime) End If End Sub Public Overrides ReadOnly Property EditType() As Type Get ' Return the type of the editing control 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) Try ' This will throw an exception of the string is ' null, empty, or not in the format of a date. Me.Value = DateTime.Parse(CStr(value)) Catch ' In the case of an exception, just use the default ' value so we're not left with a null value. Me.Value = DateTime.Now End Try 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 Not dataGridViewWantsInputKey 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
ตอบทั้งหมด
-
6 เมษายน 2555 2:39ผู้ดูแล
Hi brianm888,
Welcome to the MSDN forum.
According to your code, it seems that you customer a calendar column to datagridview. I’m afraid that more information will be needed to solve your issue.
I just confused by where is the combobox, a combobox column in datagridview or just a combobox control. If just a combobox control, which calendar cell will be influenced by it, the select one or all calendar cells in the column?
I assume you use a combobox to manage the data to one of the calendar cell (the cell is selected or the record is select). You can use the ComboBox.SelectedIndexChanged Event of combobox to get the selected item and make the special datetime with the select item, and send the datetime to the selected calendar cell.
Hope this helps.
Mark Liu-lxf [MSFT]
MSDN Community Support | Feedback to us
- ทำเครื่องหมายเป็นคำตอบโดย Mark Liu-lxfModerator 12 เมษายน 2555 7:45
- ยกเลิกการทำเครื่องหมายเป็นคำตอบโดย brianm888 16 เมษายน 2555 16:12
-
12 เมษายน 2555 7:47ผู้ดูแล
Hi brianm888,
We haven’t heard from you for several days. I’d like to mark my reply as answer firstly. If you have any additional questions, you also can unmark the replay and post your question here.
Sorry for any inconvenience and have a nice day.
Mark Liu-lxf [MSFT]
MSDN Community Support | Feedback to us
-
16 เมษายน 2555 16:12
Thanks Mark,
Apologies for not updated the thread. I'm still stuck on this problem.
I have a DataGridView with a comboBox Calendar Column.
[quote]
I assume you use a combobox to manage the data to one of the calendar cell (the cell is selected or the record is select). You can use the ComboBox.SelectedIndexChanged Event of combobox to get the selected item and make the special datetime with the select item, and send the datetime to the selected calendar cell.
[/quote]
Can you expand on this further if you get a chance? Ive tried to amend my code (see below) but Im clearly doing something wrong. I have an ComboBox (cmb1) which is outside the DataGridView as well as the ComboBox Calendar Column (cmbCalendar).
Im trying to assign the date in the external ComboBox(cmd1) to the Calendar Cell(cmdCalendar), only if the user selects a value from that comboBox(cmd1) ELSE keep the date.
Hope that makes sense. The error im receiving is "Reference to a non shared member requires an object reference".
Here's the code
Public Property EditingControlFormattedValue() As Object Implements IDataGridViewEditingControl.EditingControlFormattedValue Get Return Me.Value.ToShortDateString() End Get Set(ByVal value As Object) Try ' This will throw an exception of the string is ' null, empty, or not in the format of a date. Me.Value = DateTime.Parse(CStr(value)) Catch ' In the case of an exception, just use the default ' value so we're not left with a null value. If ComboBox1.selectedindex = -1 Then Me.Value = DateTime.Now Else Me.Value = ComboBox1.SelectedValue End If End Try End Set End Property
-
17 เมษายน 2555 6:30ผู้ดูแล
Hi brianm888,
I think the error message have less relationship with the issue in this thread.
Please check the official article about Reference to a non-shared member requires an object reference: http://msdn.microsoft.com/en-us/library/zwwhc0d0(v=vs.90).aspx
You have referenced a non-shared member within your code and failed to supply an object reference. You cannot use the class name itself to qualify a member that is not shared. The instance must first be declared as an object variable and then referenced by the variable name.
As for your code, I’m afraid that you can’t add the If statement in the property. You need to add the code about changing the date to the ComboBox.SelectedIndexChanged Event as I mentioned. In this way, when the items of combobox changed, the select data in datagridview will be changed. There also has the sample about how to use ComboBox.SelectedIndexChanged Event in the link.
Hope this helps.
Mark Liu-lxf [MSFT]
MSDN Community Support | Feedback to us
-
17 เมษายน 2555 13:32
Hi Mark,
Thank you very much for your help and links. I used the below piece of code and it works ok.
Basically I concatenated my month and year values and parsed this string into date format.
Then put an if statement in the DefaultNewRowValue property of the CalendarCell class which decides whether to use Date.Now or lblMonth.Text in the cell.
The first two lines of the code are in a seperate event handler. lblMonth.Text is set to null on page load. When the user selects a month from my ComboBox then lblMonth.Text = Cstr(Date.Parse(testDate))
Maybe its not the ideal way to do it but it appears to be working correctly.
Thanks
Dim testDate As String = "1/" & valMonth & "/" & Form1.lblYearTime.Text lblMonth.Text = CStr(Date.Parse(testDate)) Public Overrides ReadOnly Property DefaultNewRowValue() As Object Get ' Use the current date and time as the default value. If Form2.lblMonth.Text = "" Then Return Date.Now Else Return MotorMileage.lblMonth.Text End If End Get End Property