CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2012
    Posts
    7

    (VS 2010) DataGridView reading from cell

    Hey,

    I'm faced with a problem that I cannot find a solution to.

    My DGV is binded to an access db. It contains 2 columns (Date, Time), which are in the DateTime format. Everything else is a String. Now, on my CellValidating event, I go through each cell when creating a new row. What I made the program do is copy the date cell, and copy it into the time cell, which is before the time cell. As you can see on the following picture, the time cell is wrong... It should be as "11/07/2012 12:00 AM" instead of "01/01/0001 12:00 AM". Can anyone guide me in the right direction?



    PHP Code:
        Private Sub TimeLogDataGridView_CellValidating(ByVal sender As ObjectByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgsHandles TimeLogDataGridView.CellValidating

            Dim Cell 
    As DataGridViewCell TimeLogDataGridView.Item(e.ColumnIndexe.RowIndex)

            
    ' cell validation, to check if previous entry is OK

            If Cell.IsInEditMode = True Then
                Select Case TimeLogDataGridView.Columns(e.ColumnIndex).DataPropertyName

                    Case "Description"
                        If e.FormattedValue = "" Then
                            MsgBox("Enter a description.")
                            e.Cancel = True
                            Cell.Selected = True
                        Else
                            e.Cancel = False
                        End If

                    Case "currentDate"
                        If e.FormattedValue = "" Then
                            MsgBox("Enter a date.")
                            e.Cancel = True
                            Cell.Selected = True
                        Else
                            e.Cancel = False

                            Me.TimeLogDataGridView.Columns(2).DefaultCellStyle.Format = "g"
                            ' 
    this is for testing
                            Me
    .TimeLogDataGridView.CurrentRow.Cells(3).Value TimeLogDataGridView.CurrentRow.Cells(0).Value.ToString
                            
    ' this is screwed up
                            Me.TimeLogDataGridView.CurrentRow.Cells(2).Value = TimeLogDataGridView.CurrentRow.Cells(1).Value.ToString
                            ' 
    testing 2
                            Me
    .Label4.Text TimeLogDataGridView.CurrentRow.Cells(2).Value.ToString

                        End 
    If
                    Case 
    "timeDate"
                        
    If e.FormattedValue <> "" Then
                            
    If IsDate(e.FormattedValue) = True Then
                                e
    .Cancel False
                            
    Else
                                
    MsgBox("Enter a time.")
                                
    e.Cancel True
                                Cell
    .Selected True
                            End 
    If
                        
    End If
                
    End Select
            End 
    If
        
    End Sub 

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: (VS 2010) DataGridView reading from cell

    Because you have a TIME/DATE field. Use # # around it in the format it expects. ddd. and the remainder as a fraction.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

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

Featured