|
-
Apr 25th, 2019, 12:03 AM
#1
Thread Starter
Member
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")
-
Apr 25th, 2019, 05:37 AM
#2
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.
-
Apr 25th, 2019, 07:25 AM
#3
Re: Datagrid set value as Date
 Originally Posted by gbfai87
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
-
Apr 25th, 2019, 07:38 PM
#4
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:
DGVEmployee.DataSource = myDataTable TBJoinDate.DataBindings.Add("Text", myDataTable, "JoinDate").FormatString = "dd-MMM-yyyy"
-
Apr 25th, 2019, 07:40 PM
#5
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:
TBJoinDate.Text = If(DGV.Item(4, i).Value Is DBNull.Value, String.Empty, CDate(DGV.Item(4, i).Value).ToString("dd-MMM-yyyy"))
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|