none
Unable to set a date value in a datagridview, says dbnull is readOnly RRS feed

  • Question

  • I have a datagridview where I add a new row, and populate the row.   This has worked until now, when I added a date field to the datagridview.

    My code looks like this (excerpts)

     Dim dc7 As New DataColumn
            With dc7
                .AllowDBNull = True
                .Caption = "Published"
                .ColumnName = "publishdate"
                .DataType = Type.GetType("System.DateTime")
    
                .ReadOnly = False
            End With
            dtScanFolderGlobal.Columns.Add(dc7)
    
     dr = dtfromfileLocal.NewRow
                    strPubDate = wordarray(6)
                    If strPubDate.Length > 0 Then
                        If Date.TryParse(strPubDate, pubDate) Then
                            dr("publishdate").value = pubDate 
                        End If
                    End If

    I used F9 to step through it, and the date is a real date.   But the message I get is:

    Field 'Value' of type 'DBNull' is 'ReadOnly'.

    Why would this happen?

    Thursday, December 28, 2017 2:45 PM

Answers

  • Hi

    Not to sure of this, but try forcing a data type on pubdate with

    dr("publishdate").value = cdate(pubDate) 

    EDIT: you say in the thread title that you are working with a DataGridView but in the code you show you use DataColumn when it should be DataGridViewColumn (if you are actually using a DataGridView rather than a DataGrid)

    Here is some simple code that sets up a DataGridView1 for a single column of DateTime.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    	With DataGridView1
    	  .Columns.Add("publishdate", "Published")
    	  .Columns("publishdate").DefaultCellStyle.Format = "dd-MMM-yyyy"
    	End With
    
    	DataGridView1.Rows.Add(Now.AddDays(-2))
    	DataGridView1.Rows.Add(Now.AddDays(-3))
    	DataGridView1.Rows.Add(Now.AddDays(-4))
      End Sub
    End Class
    


    Regards Les, Livingston, Scotland


    • Edited by leshay Thursday, December 28, 2017 4:37 PM
    • Marked as answer by Gidmaestro Thursday, December 28, 2017 8:58 PM
    Thursday, December 28, 2017 3:27 PM

All replies

  • Hi

    Not to sure of this, but try forcing a data type on pubdate with

    dr("publishdate").value = cdate(pubDate) 

    EDIT: you say in the thread title that you are working with a DataGridView but in the code you show you use DataColumn when it should be DataGridViewColumn (if you are actually using a DataGridView rather than a DataGrid)

    Here is some simple code that sets up a DataGridView1 for a single column of DateTime.

    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    	With DataGridView1
    	  .Columns.Add("publishdate", "Published")
    	  .Columns("publishdate").DefaultCellStyle.Format = "dd-MMM-yyyy"
    	End With
    
    	DataGridView1.Rows.Add(Now.AddDays(-2))
    	DataGridView1.Rows.Add(Now.AddDays(-3))
    	DataGridView1.Rows.Add(Now.AddDays(-4))
      End Sub
    End Class
    


    Regards Les, Livingston, Scotland


    • Edited by leshay Thursday, December 28, 2017 4:37 PM
    • Marked as answer by Gidmaestro Thursday, December 28, 2017 8:58 PM
    Thursday, December 28, 2017 3:27 PM
  • You show that you have a datatable (I assume) with the name dtScanFolderGlobal and one with the name dtfromfileLocal

    Is there a relation between those 2, otherwise adding a column to the first and adding a value to the second makes no sense. 


    Success Cor

    Thursday, December 28, 2017 4:07 PM
  • One table is a clone of the other (made via the clone method)
    Thursday, December 28, 2017 8:58 PM