Results 1 to 27 of 27

Thread: [2005] Create New DatagridViewColumn control

  1. #1

    Thread Starter
    Fanatic Member staticbob's Avatar
    Join Date
    Jan 2005
    Location
    Manchestershire, UK Cabbage: I do
    Posts
    619

    [2005] Create New DatagridViewColumn control

    Guys,

    I am trying to create a new datagridcolumn control. I need a datetimepicker, with up/down spinner, that allows me to set starttime, endtime and interval. I only need to change the time, not the date.

    I have a CalendarColumn working, thanks to some links from jmcilhinney, but this shows a dropdown calendar control.

    I'm thinking there must be some way of combining what I have already with something like this, but its way past my ability.

    Can anybody help / advise on this ?

    Thanks
    Bob
    "I dislike 7 am. If 7 am were a person, I would punch 7 am in the biscuits." - Paul Ryan, DailyRamblings

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Create New DatagridViewColumn control

    Not you again! Like I said, you need to propagate the desired properties of the editing control through the cell to the column. I'll post my classes as they are at the moment so you can see what I've done. You may even be able to use them "as is". I'm not 100% sure of how far I got with them because I've been concentrating on a MaskedTextBox column recently. Note that the code is in C# so you can either compile them in C# Express, convert them using a code converter or else just use them to get some ideas and write your own code for your own classes.
    Attached Files Attached Files
    Last edited by jmcilhinney; Jun 20th, 2006 at 06:55 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Create New DatagridViewColumn control

    There's one more thing to note. I created a customised version of the TextBox column and cell classes to allow me to specify my own error icon. Those classes were included in the same namespace but I think you'll find that if they are not present the standard classes will be used seamlessly. If I remember correctly you would have been completely unaware of the difference but I thought I'd mention it in case I didn't remember correctly.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Fanatic Member staticbob's Avatar
    Join Date
    Jan 2005
    Location
    Manchestershire, UK Cabbage: I do
    Posts
    619

    Re: [2005] Create New DatagridViewColumn control

    Thanks John, do you ever sleep ???

    Can I get your "ConversionHelpers" class ? Please ? So I can use it in cases like this, rather than me trying to understand it ?

    cell = CType(ConversionHelpers.AsWorkaround(rows.SharedRow(i).Cells(Me.Index), GetType

    Thanks again.
    Bob

    EDIT - Attached converted code.
    Attached Files Attached Files
    Last edited by staticbob; Jun 21st, 2006 at 05:56 AM.
    "I dislike 7 am. If 7 am were a person, I would punch 7 am in the biscuits." - Paul Ryan, DailyRamblings

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Create New DatagridViewColumn control

    I'm not sure what you used to convert the C# code but whatever it was it invented that ConversionHelpers class itself because it's nothing to do with me. It seems to have used it wherever the C# "as" keyword was used. For instance, the line you quote above looks like this in your converted code:
    VB Code:
    1. cell = CType(ConversionHelpers.AsWorkaround(rows.SharedRow(i).Cells(Me.Index), GetType(DataGridViewDateTimePickerCell)), DataGridViewDateTimePickerCell)
    while it looked like this in my original code:
    Code:
    cell = rows.SharedRow(i).Cells[this.Index] as DataGridViewDateTimePickerCell;
    That would have been necessary in VB.NET 2003 but VB 2005 adds the TryCast function so the easy conversion is this:
    VB Code:
    1. cell = TryCast(rows.SharedRow(i).Cells(Me.Index), DataGridViewDateTimePickerCell)
    Note also that you don't have to include all the functionality that I have. For instance, you may not need the Checked, ShowCheckBox, MaxDate, MinDate, ZeroTime and ErrorIcon properties.
    Last edited by jmcilhinney; Jun 21st, 2006 at 06:18 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Fanatic Member staticbob's Avatar
    Join Date
    Jan 2005
    Location
    Manchestershire, UK Cabbage: I do
    Posts
    619

    Re: [2005] Create New DatagridViewColumn control

    John,

    Trying to compile the C# code directly now, just one error to sort out and its a simple one... what do I need to reference ?

    Bob


    "Error 1 The type or namespace name 'ToolboxBitmap' could not be found (are you missing a using directive or an assembly reference?) C:\Projects\DataGridViewDTPColumn\DataGridViewDateTimePickerColumn.cs 20 6 DataGridViewDTPColumn"
    "I dislike 7 am. If 7 am were a person, I would punch 7 am in the biscuits." - Paul Ryan, DailyRamblings

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Create New DatagridViewColumn control

    Here's how you find the answer to that yourself. That ToolboxBitmap is an attribute and the actual class for attributes always ends in "Attribute", so the actual class is ToolboxBitmapAttribute. You go to the MSDN library and search for "toolboxbitmapattribute class". You read the class overview topic that is retruned in the results and it tells you that it is the System.Drawing.ToolboxBitmapAttribute class. I've imported that namespace at the top of my column class file.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Fanatic Member staticbob's Avatar
    Join Date
    Jan 2005
    Location
    Manchestershire, UK Cabbage: I do
    Posts
    619

    Re: [2005] Create New DatagridViewColumn control

    I thankyou.
    "I dislike 7 am. If 7 am were a person, I would punch 7 am in the biscuits." - Paul Ryan, DailyRamblings

  9. #9

    Thread Starter
    Fanatic Member staticbob's Avatar
    Join Date
    Jan 2005
    Location
    Manchestershire, UK Cabbage: I do
    Posts
    619

    Re: [2005] Create New DatagridViewColumn control

    And the OnProperty change ?

    Error 1 'Wunnell.Windows.Forms.DataGridViewDateTimePickerCell' does not contain a definition for 'OnPropertyChange'

    Any particular reason why private void OnPropertyChange() is already commented out ?

    EDIT :: It builds fine if I stick it back in. The only think I have removed is the erroricon stuff.
    "I dislike 7 am. If 7 am were a person, I would punch 7 am in the biscuits." - Paul Ryan, DailyRamblings

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Create New DatagridViewColumn control

    Ah, that's because I implemented that method in my custom TextBox cell class. I had it in the DateTimePicker cell class originally but when I decided to inherit my own class instead of the standard I commented it out. You can just uncomment it, or you're welcome to my TextBox column classes as well if you like. All it adds at the moment is the ability to specify your own error icon, which is then inherited by my other columns. I intend to add some other properties, like PasswordChar and CharacterCasing, in the not too distant future.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Fanatic Member staticbob's Avatar
    Join Date
    Jan 2005
    Location
    Manchestershire, UK Cabbage: I do
    Posts
    619

    Re: [2005] Create New DatagridViewColumn control

    I'll have it when its finished John.

    Thanks for the code and help, I'm having a go at implementing it in my project now.
    "I dislike 7 am. If 7 am were a person, I would punch 7 am in the biscuits." - Paul Ryan, DailyRamblings

  12. #12

    Thread Starter
    Fanatic Member staticbob's Avatar
    Join Date
    Jan 2005
    Location
    Manchestershire, UK Cabbage: I do
    Posts
    619

    Re: [2005] Create New DatagridViewColumn control

    Me again,

    I have the control working in context now, with a spinner rather than calendar dropdown.

    Could you please give me some pointers as to how I can now...

    - Have a min and max TIME setting, 6am - 8pm, the date portion should be assigned from a variable at runtime _dDiaryDate.
    - Make the up/down increment the time in 15min intervals, rather than it only affecting the time portion active. (hours or minutes)

    Thanks again,
    Bob
    Last edited by staticbob; Jun 22nd, 2006 at 06:12 AM.
    "I dislike 7 am. If 7 am were a person, I would punch 7 am in the biscuits." - Paul Ryan, DailyRamblings

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Create New DatagridViewColumn control

    1. Once you've set the date portion you then set the MinDate and MaxDate based on that date:
    VB Code:
    1. Me.DateTimePicker1.MinDate = Me.DateTimePicker1.Value.Date.AddHours(6)
    2. Me.DateTimePicker1.MaxDate = Me.DateTimePicker1.Value.Date.AddHours(20)
    Note that if you want to change the date portion again at any stage you'll have to remove either or both of those limits and then reset them again afterwards or else you'll get an exception thrown. Note also that you'll have to do this individually for each cell if they are to have different dates.

    2. That's not how the DateTimePicker works. If you want that behaviour then you're going to have to code it in your editing control class. Be warned though that you may run into some classes and methods that are in the System.Windows.Forms.dll assembly and declared Friend, thus you cannot use them and will have to reproduce their behaviour yourself. I used Reflector a lot while I was coding my columns to see how the Framework classes actually work.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14

    Thread Starter
    Fanatic Member staticbob's Avatar
    Join Date
    Jan 2005
    Location
    Manchestershire, UK Cabbage: I do
    Posts
    619

    Re: [2005] Create New DatagridViewColumn control

    Morning John,

    I have it working almost as needed. How can I do this....


    VB Code:
    1. Private Sub dgv_Visitors_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) _
    2. Handles dgv_Visitors.EditingControlShowing
    3.  
    4.         Dim dtp_starttime As JMCDataGridViewDateTimePickerColumn = dgv_Visitors.Columns(4)
    5.         Dim dtp_endtime As JMCDataGridViewDateTimePickerColumn = dgv_Visitors.Columns(5)
    6.         Select Case Me.dgv_Visitors.CurrentCellAddress.X
    7.             Case dtp_endtime.Index
    8.                 'Set the MinDate for this cell only to be the value
    9.                 'from dtp_starttime for this row
    10.  
    11.                 'AddHandler DirectCast(e.Control, DateTimePicker).CloseUp, AddressOf dtp_timechanged
    12.         End Select
    13.  
    14.  
    15.     End Sub

    I want to make sure the users can't select a time in ENDTime that is before STARTTime

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

    Also, how can I make the values in a new row blank, until the user clicks into the control. At the moment, a new row has datetime.now, as set here...
    Sorry, can't quite grasp whats going on here with the showCheckBox etc etc


    Code:
    public override object DefaultNewRowValue
            {
                get
                {
                    if (this.IsInEditMode || !this._showCheckBox || this._checked)
                    {
                        //DateTime.Now : 
                        // The default value for a populated cell is the current date and, optionally, time.
                        return this.ZeroTime ? DateTime.Now : DateTime.Now;
                    }
                    else
                    {
                        // An unchecked check box in a cell that is not being edited indicates a null value.
                        return DBNull.Value;
                    }
                }
            }
    Bob
    Last edited by staticbob; Jun 22nd, 2006 at 10:00 AM.
    "I dislike 7 am. If 7 am were a person, I would punch 7 am in the biscuits." - Paul Ryan, DailyRamblings

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Create New DatagridViewColumn control

    Just like the regular DateTimePicker control, if you do not display the check box in the control then it must always have a value. The check box is what allows you to specify a null value, by unchecking it. A cell must have its ShowCheckBox property set to True and its Checked property set to False in order to have a null value.

    As for your first question, I'd be inclined to handle the CellValueChanged event of the grid. You then test the ColumnIndex to see if you're in the StartTime column. If you are then you use the Value of that cell to set the MinDate property of the cell in the same row in the EndTime column. You may similarly use the Value in the EndTime column to set the MaxDate property of the StartTime column. If you don't then setting a StartTime after the current EndTime should automatically adjust the EndTime value too.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  16. #16

    Thread Starter
    Fanatic Member staticbob's Avatar
    Join Date
    Jan 2005
    Location
    Manchestershire, UK Cabbage: I do
    Posts
    619

    Re: [2005] Create New DatagridViewColumn control

    Thanks John,

    How could I do this, I can't see it at the moment....

    "set the MinDate property of the cell in the same row in the EndTime column."
    "I dislike 7 am. If 7 am were a person, I would punch 7 am in the biscuits." - Paul Ryan, DailyRamblings

  17. #17
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Create New DatagridViewColumn control

    You got the row index already because its the same as the current cell. You know the column index of the column that contains the end time. That gives you the cell. Cast it as the appropriate type and set it MinDate property.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  18. #18

    Thread Starter
    Fanatic Member staticbob's Avatar
    Join Date
    Jan 2005
    Location
    Manchestershire, UK Cabbage: I do
    Posts
    619

    Re: [2005] Create New DatagridViewColumn control

    OK, so I'm trying this....

    VB Code:
    1. Private Sub dgv_Visitors_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv_Visitors.CellValueChanged
    2.  
    3.         Try
    4.             If e.RowIndex < 0 Then Return
    5.  
    6.             Dim dtpcell As DataGridViewDateTimePickerCell
    7.  
    8.             Select Case e.ColumnIndex
    9.                 Case 4 ' Time In
    10.                     dtpcell = dgv_Visitors(5, e.RowIndex)
    11.                     dtpcell.Value = dgv_Visitors(e.ColumnIndex, e.RowIndex).Value
    12.                     dtpcell.MinDate = dgv_Visitors(e.ColumnIndex, e.RowIndex).Value
    13.                 Case 5 ' Time Out
    14.                     dtpcell = dgv_Visitors(4, e.RowIndex)
    15.                     dtpcell.Value = dgv_Visitors(e.ColumnIndex, e.RowIndex).Value
    16.                     dtpcell.MaxDate = dgv_Visitors(e.ColumnIndex, e.RowIndex).Value
    17.             End Select
    18.         Catch ex As Exception
    19.             PWBlib.EH.Log(ex)
    20.         End Try
    21.  
    22.     End Sub

    But I am getting an error when changing the first timecolumn (4).
    Code:
    Error: Value of '23/06/2006 00:00:00' is not valid for 'MaxDate'. 'MaxDate' must be greater than or equal to MinDate.
    Parameter name: MaxDate
    Err No: 5
    Exception: ArgumentOutOfRangeException
    Stack Trace: 
       at System.Windows.Forms.DateTimePicker.set_MaxDate(DateTime value)
       at Wunnell.Windows.Forms.DataGridViewDateTimePickerCell.SetMaxDate(Int32 rowIndex, DateTime value) in C:\_VSProjects\DataGridViewDTPColumn\DataGridViewDTPColumn\DataGridViewDateTimePickerCell.cs:line 767

    How do I stop the event firing again when I'm setting the value of the 2nd time column ? That seems to be the problem...
    "I dislike 7 am. If 7 am were a person, I would punch 7 am in the biscuits." - Paul Ryan, DailyRamblings

  19. #19

    Thread Starter
    Fanatic Member staticbob's Avatar
    Join Date
    Jan 2005
    Location
    Manchestershire, UK Cabbage: I do
    Posts
    619

    Re: [2005] Create New DatagridViewColumn control

    Hello again,

    I'm still having slight issues with this control. This is the only code I have interacting with my datagridview at the moment....

    VB Code:
    1. Private Sub dgv_Visitors_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv_Visitors.CellValueChanged
    2.  
    3.         Try
    4.             If e.RowIndex < 0 Then Return
    5.             If e.ColumnIndex < 4 Or e.ColumnIndex > 5 Then Return
    6.             If _bdgvediting Then Return
    7.  
    8.             Dim dtpcell As DataGridViewDateTimePickerCell
    9.             Select Case e.ColumnIndex
    10.                 Case 4 ' Time In
    11.                     dtpcell = dgv_Visitors(5, e.RowIndex)
    12.                     _bdgvediting = True
    13.                     dtpcell.MinDate = dgv_Visitors(e.ColumnIndex, e.RowIndex).Value
    14.                     dtpcell.Value = dgv_Visitors(e.ColumnIndex, e.RowIndex).Value
    15.                     _bdgvediting = False
    16.                 Case 5 ' Time Out
    17.                     dtpcell = dgv_Visitors(4, e.RowIndex)
    18.                     _bdgvediting = True
    19.                     dtpcell.MaxDate = dgv_Visitors(e.ColumnIndex, e.RowIndex).Value
    20.                     _bdgvediting = False
    21.             End Select
    22.         Catch ex As Exception
    23.             PWBlib.EH.Log(ex)
    24.         End Try
    25.  
    26.     End Sub

    ... just trying to set the min&max dates according to the times selected. This is partially working. I am seeing this error when modifying col(5)...

    Code:
    Assertion Failed: Abort=Quit, Retry=Debug, Ignore=Continue
    ---------------------------
    
        at DataGridViewDateTimePickerCell.Constrain(DateTime value)  C:\_VSProjects\DataGridViewDTPColumn\DataGridViewDTPColumn\DataGridViewDateTimePickerCell.cs(446)
    
        at DataGridViewDateTimePickerCell.InitializeEditingControl(Int32 rowIndex, Object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)  C:\_VSProjects\DataGridViewDTPColumn\DataGridViewDTPColumn\DataGridViewDateTimePickerCell.cs(609)
    
        at DataGridView.InitializeEditingControlValue(DataGridViewCellStyle& dataGridViewCellStyle, DataGridViewCell dataGridViewCell)  
    
        at DataGridView.BeginEditInternal(Boolean selectAll)  
    
        at DataGridView.SetCurrentCellAddressCore(Int32 columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean throughMouseClick)  
    
        at DataGridView.OnCellMouseDown(HitTestInfo hti, Boolean isShiftDown, Boolean isControlDown)  
    
        at DataGridView.OnCellMouseDown(DataGridViewCellMouseEventArgs e)  
    
        at DataGridView.OnMouseDown(MouseEventArgs e)  
    
        at Control.WmMouseDown(Message& m, MouseButtons button, Int32 clicks)  
    
        at Control.WndProc(Message& m)  
    
        at DataGridView.WndProc(Message& m)  
    
        at ControlNativeWindow.OnMessage(Message& m)  
    
        at ControlNativeWindow.WndProc(Message& m)  
    
        at NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)  
    
        at UnsafeNativeMethods.DispatchMessageW(MSG& msg)  
    
        at ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)  
    
        at ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)  
    
        at ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)  
    
        at Form.ShowDialog(IWin32Window owner)  
    
        at Form.ShowDialog()  
    
        at ProjectWorkbook.main()  C:\_VSProjects\PWBV3\Modules\ProjectWorkbook.vb(55)

    The only change I have made to the control code is....

    Code:
      if (this.IsInEditMode || !this._showCheckBox || this._checked)
                    {
                        //DateTime.Now : 
                        // The default value for a populated cell is the current date and, optionally, time.
                        return this.ZeroTime ? this.MinDate : this.MinDate; // DateTime.Now : DateTime.Now;
                    }
                    else

    Can you see any problems with this ?
    I have attached the c# control code as it stands now.
    Attached Files Attached Files
    "I dislike 7 am. If 7 am were a person, I would punch 7 am in the biscuits." - Paul Ryan, DailyRamblings

  20. #20

    Thread Starter
    Fanatic Member staticbob's Avatar
    Join Date
    Jan 2005
    Location
    Manchestershire, UK Cabbage: I do
    Posts
    619

    Re: [2005] Create New DatagridViewColumn control

    Hmmmm,

    After drilling down into the values at runtime, I think this could be a problem....
    ------------
    ?dgv_Visitors(4, e.RowIndex).Value
    #6/26/2006# {Date}
    Date: #6/26/2006#
    ------------
    ?ctype(dgv_Visitors(e.ColumnIndex, e.RowIndex).Value,DateTime).TimeOfDay
    {System.TimeSpan}
    Days: 0
    Hours: 0
    MaxValue: {System.TimeSpan}
    Milliseconds: 0
    Minutes: 0
    MinValue: {System.TimeSpan}
    Seconds: 0
    Ticks: 0
    --------------

    - No time ???

    Strange that this only occurs on Case 5, Case 4 works fine ?

    Bob
    "I dislike 7 am. If 7 am were a person, I would punch 7 am in the biscuits." - Paul Ryan, DailyRamblings

  21. #21
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Create New DatagridViewColumn control

    It says an assertion is failing in the Constrain method so put a breakpoint on that assertion and see what the problem is.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  22. #22
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Create New DatagridViewColumn control

    The assertion in the Constrain method is:
    Code:
    Debug.Assert(this._minDate <= this._maxDate);
    so you're messing up the limits somewhere.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  23. #23

    Thread Starter
    Fanatic Member staticbob's Avatar
    Join Date
    Jan 2005
    Location
    Manchestershire, UK Cabbage: I do
    Posts
    619

    Re: [2005] Create New DatagridViewColumn control

    John, any idea why this is happening ? (See attached .avi)

    - A new row appears with the default value, mindate. (6:00am)
    - As soon as you begin edit on that new row, the values from the dtp column disappear. (I have the datagridview set to "EditonEnter")
    - When I then edit these columns, NOW shows, but if I click out of the cell without making a change it disappears again.
    - When moving between the columns if I don't make changes, I get an error, At the time of the error, both Min and Max date have zeor time values, which I just can't understand.

    Code:
    System.ArgumentOutOfRangeException was unhandled by user code
      Message="Value of '27/06/2006 06:00:00' is not valid for 'Value'. 'Value' should be between 'MinDate' and 'MaxDate'.\r\nParameter name: Value"
      Source="System.Windows.Forms"
      ParamName="Value"
      StackTrace:
           at System.Windows.Forms.DateTimePicker.set_Value(DateTime value)
           at Wunnell.Windows.Forms.DataGridViewDateTimePickerCell.InitializeEditingControl(Int32 rowIndex, Object initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle) in C:\_VSProjects\DataGridViewDTPColumn\DataGridViewDTPColumn\DataGridViewDateTimePickerCell.cs:line 605
           at System.Windows.Forms.DataGridView.InitializeEditingControlValue(DataGridViewCellStyle& dataGridViewCellStyle, DataGridViewCell dataGridViewCell)
    This is the only code I have interacting with the dgv...

    This On LOAD
    VB Code:
    1. 'Set-up DTP columns
    2.         Dim dtp_starttime As JMCDataGridViewDateTimePickerColumn = dgv_Visitors.Columns(4)
    3.         Dim dtp_endtime As JMCDataGridViewDateTimePickerColumn = dgv_Visitors.Columns(5)
    4.  
    5.         dtp_starttime.MinDate = Me.dtp_diarydate.Value.Date.AddHours(6)
    6.         dtp_starttime.MaxDate = Me.dtp_diarydate.Value.Date.AddHours(20)
    7.         dtp_endtime.MinDate = Me.dtp_diarydate.Value.Date.AddHours(6)
    8.         dtp_endtime.MaxDate = Me.dtp_diarydate.Value.Date.AddHours(20)

    And This...

    VB Code:
    1. Private Sub dgv_Visitors_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
    2.     Handles dgv_Visitors.CellValueChanged
    3.  
    4.         Try
    5.             If e.RowIndex < 0 Then Return
    6.             If e.ColumnIndex < 4 Then Return
    7.             If _bdgvediting Then Return
    8.  
    9.             Dim dtpcell As DataGridViewDateTimePickerCell
    10.            
    11.             dtpcell = dgv_Visitors(5, e.RowIndex)
    12.             _bdgvediting = True
    13.             dtpcell.MinDate = Nz(dgv_Visitors(e.ColumnIndex, e.RowIndex).Value, Now)
    14.             dtpcell.Value = CType(Nz(dgv_Visitors(e.ColumnIndex, e.RowIndex).Value, Now), Date).Date.AddHours(1)
    15.             _bdgvediting = False
    16.  
    17.             'nz is my Null value catch.....
    18.             'Public Overloads Function Nz(ByVal Field As Object, ByVal ValIfNull As Date) As Date
    19.             '    If IsDBNull(Field) Then
    20.             '        Return ValIfNull
    21.             '    Else
    22.             '        Return CDate(Field)
    23.             '    End If
    24.             'End Function
    25.  
    26.         Catch ex As Exception
    27.             PWBlib.EH.Log(ex)
    28.         End Try
    29.  
    30.     End Sub

    I just can't see why I'm getting this error, nor can I understand why I "Loose" the default entries on row edit. I have re-created this grid in a stand alone app and the default remains when you edit a new row.

    Help, again please !

    Bob
    Attached Files Attached Files
    Last edited by staticbob; Jun 30th, 2006 at 04:48 AM.
    "I dislike 7 am. If 7 am were a person, I would punch 7 am in the biscuits." - Paul Ryan, DailyRamblings

  24. #24

    Thread Starter
    Fanatic Member staticbob's Avatar
    Join Date
    Jan 2005
    Location
    Manchestershire, UK Cabbage: I do
    Posts
    619

    Re: [2005] Create New DatagridViewColumn control

    John,

    I have a problem with this.....

    Code:
    public override object DefaultNewRowValue
            {
                get
                {
                    if (this.IsInEditMode || !this._showCheckBox || this._checked)
                    {
                        //DateTime.Now : 
                        // The default value for a populated cell is the current date and, optionally, time.
                        return this.ZeroTime ? DateTime.Today : DateTime.Now;
    
                    }
                    else
                    {
                        // An unchecked check box in a cell that is not being edited indicates a null value.
                        return this.MinDate;
                    }
                }
            }


    Whenever using this control, the first thing my users do is select the date they are completing the report for. The default is the current date, but they could, using a dtp control, select a date in the past to complete the report for. Changing the DTP value sets a public _dDiaryDate on my usercontrol.

    It also does this...
    VB Code:
    1. Dim dtp_starttime As JMCDataGridViewDateTimePickerColumn = dgv_Visitors.Columns(4)
    2.         Dim dtp_endtime As JMCDataGridViewDateTimePickerColumn = dgv_Visitors.Columns(5)
    3.  
    4.         dtp_starttime.MinDate = Me.dtp_diarydate.Value.Date.AddHours(6)
    5.         dtp_starttime.MaxDate = Me.dtp_diarydate.Value.Date.AddHours(20)
    6.         dtp_endtime.MinDate = Me.dtp_diarydate.Value.Date.AddHours(6)
    7.         dtp_endtime.MaxDate = Me.dtp_diarydate.Value.Date.AddHours(20)


    So, I need the defaultnewrowvalue, to look at this rather than DateTime.Today : DateTime.Now

    How on earth can I get your class to see a variable in my exe project at runtime ? Or my exe to also set the defaulynewdate in your class ?

    Thanks !
    Bob
    "I dislike 7 am. If 7 am were a person, I would punch 7 am in the biscuits." - Paul Ryan, DailyRamblings

  25. #25
    New Member
    Join Date
    Oct 2006
    Posts
    7

    Re:Create New DatagridViewColumn control using vb.net 2005 Express

    Has anyone made any further progress on this CalendarColumn control?

    I'm been attempting to extend this control a bit further over the last few weeks but have stopped and started so many times I've become lost.

    i.e. bound to a database.
    null dates after Enable Adding.
    Having the calendarcolumn blank before row add.
    Cancelling the add.
    Setting min and max dates on control initialization.
    Committing changes on row index change.
    etc.

    Any further positive progressive help would be greatly appreciated?
    Last edited by BarrySumpter; Oct 22nd, 2006 at 04:34 AM.

  26. #26
    New Member
    Join Date
    Nov 2007
    Posts
    2

    Re: [2005] Create New DatagridViewColumn control

    Hello,

    would you accept to share the code you are talking about : allowing to manage dateTimePicker in datagridview, with minDate, maxDate , etc... features ?

    I tried this code :http://msdn2.microsoft.com/en-us/library/7tas5c80.aspx)

    it works fine, but I can't find a way to add the minDate, maxDate feature, to the dateTimepicker in the datagridview.

    I develop in VB.net, but if it is in C#, I will make the translation myself

    thank you if you can help me

    Best Regards
    Olivier

  27. #27
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] Create New DatagridViewColumn control

    You need to handle the EditingControlShowing event of your datagridview and test to see if the type of e.Control is CalendarEditingControl. If it is, you cast it to CalendarEditingControl and you can access all of its members including MinDate and MaxDate... Just do a search for "DataGridView EditingControlShowing" in this forum and you should find some sample code (may be not for your specific type, but close enough that you can see how it's done).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width