Results 1 to 5 of 5

Thread: Datagrid set value as Date

  1. #1

    Thread Starter
    Member gbfai87's Avatar
    Join Date
    Oct 2017
    Posts
    33

    Datagrid set value as Date

    Hi,
    Is there any way to set the value of an item in DGV in a Date only?.

    I was trying to send each cell value in my DGV.
    but when I change the value to ".tostring" it transfer the date also with time.
    but when I change the value to format date. it doesnt accept to transfer if cell value is Null.

    here is my code.

    Dim i As Integer
    i = DGVEmployee.CurrentRow.Index
    LblID.Text = DGV.Item(0, i).Value.ToString
    TBEmpID.Text = DGV.Item(1, i).Value.ToString
    TBSponsor.Text = DGV.Item(2, i).Value.ToString
    TBDesignation.Text = DGV.Item(3, i).Value.ToString
    TBJoinDate.Text = Format(DGV.Item(4, i).Value, "dd-MMM-yyyy")

  2. #2
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,129

    Re: Datagrid set value as Date

    Hi,

    try this, you have to pass the Column Name to Format the Date

    Code:
    Public Class Form6
    
        Dim tb As DataTable = New DataTable
    
    
        Private Sub Form6_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            tb.Columns.Add("ID")
            tb.Columns.Add("Date Returned")
            'some Data
            tb.Rows.Add("1", "01.01.2018 12:00:33")
            tb.Rows.Add("2", "01.05.2018 14:00:33")
            tb.Rows.Add("3", "08.10.2018 12:00:33")
            tb.Rows.Add("4", "01.01.2012")
            DataGridView1.DataSource = tb
        End Sub
    
        Private Shared Sub ShortFormDateFormat(ByVal formatting As DataGridViewCellFormattingEventArgs)
            If formatting.Value IsNot Nothing Then
                Try
                    Dim theDate As Date = DateTime.Parse(formatting.Value.ToString())
                    Dim dateString As String = Format(theDate, "dd-MMM-yyyy ")
                    formatting.Value = dateString.ToString()
                    formatting.FormattingApplied = True
                Catch notInDateFormat As FormatException
                    formatting.FormattingApplied = False
                End Try
            End If
        End Sub
    
        Private Sub DataGridView1_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
            If Me.DataGridView1.Columns(e.ColumnIndex).Name = "Date Returned" Then
                ShortFormDateFormat(e)
            End If
        End Sub
    End Class
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Datagrid set value as Date

    Quote Originally Posted by gbfai87 View Post
    Hi,
    Is there any way to set the value of an item in DGV in a Date only?.

    I was trying to send each cell value in my DGV.
    but when I change the value to ".tostring" it transfer the date also with time.
    but when I change the value to format date. it doesnt accept to transfer if cell value is Null.

    here is my code.

    Dim i As Integer
    i = DGVEmployee.CurrentRow.Index
    LblID.Text = DGV.Item(0, i).Value.ToString
    TBEmpID.Text = DGV.Item(1, i).Value.ToString
    TBSponsor.Text = DGV.Item(2, i).Value.ToString
    TBDesignation.Text = DGV.Item(3, i).Value.ToString
    TBJoinDate.Text = Format(DGV.Item(4, i).Value, "dd-MMM-yyyy")
    Are you trying to set the formatting on the grid, or on the date coming from the grid to the text boxes? beause your code there isn't setting it ont he grid, which you shouldn't have issues with. I've never had issues with setting the format on a grid with respect to dates and null values. It should handle that just fine. So it sounds like the issue is with that last line where you're reading the value from the grid and transfering it to a text box and it's erroring out because it's null and you're explicitly trying to apply a format... which yeah, it's going to fail... soooooo.... wouldn't the obvious solution then be to check for the null value first, and if it's null, don't copy the value, and if it isn't null, do copy it and format it?


    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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

    Re: Datagrid set value as Date

    You are doing this in completely the wrong way. If you have bound your data to your grid then you can bind it to the other controls as well and let the binding handle the formatting, e.g.
    vb.net Code:
    1. DGVEmployee.DataSource = myDataTable
    2.  
    3. TBJoinDate.DataBindings.Add("Text", myDataTable, "JoinDate").FormatString = "dd-MMM-yyyy"
    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

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

    Re: Datagrid set value as Date

    Of course, if you wanted to stick with your current method and you're having issues when the data is NULL, the obvious solution is to check whether the data is NULL before using it.
    vb.net Code:
    1. TBJoinDate.Text = If(DGV.Item(4, i).Value Is DBNull.Value, String.Empty, CDate(DGV.Item(4, i).Value).ToString("dd-MMM-yyyy"))
    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

Tags for this Thread

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