RadhaKrishnaKiJaya
asked on
Datagridview format column
Hi Experts,
I have a bound datagridview. One of the column is displaying the date. Now the date is displaying as a string("20140801" ). I want to display it as mm/dd/yyyy. I also have a time (132312). I want to display in HH:MM:SS format. How can I format the columns.
Thanks.
Rina
I have a bound datagridview. One of the column is displaying the date. Now the date is displaying as a string("20140801" ). I want to display it as mm/dd/yyyy. I also have a time (132312). I want to display in HH:MM:SS format. How can I format the columns.
Thanks.
Rina
ASKER
Dim out_date As String = dgvStoreResult.Rows(i).Cel ls(1).Valu e
dgvStoreResult.Rows(i).Cel ls(1).Valu e = Format_String_ToDate(out_d ate)
Dim out_time As String = dgvStoreResult.Rows(i).Cel ls(2).Valu e
dgvStoreResult.Rows(i).Cel ls(2).Valu e = Format_String_ToTime(out_t ime)
Public Shared Function Format_String_ToTime(stk_T ime As String) As String
If stk_Time.Trim <> "" Then
If stk_Time.Trim.Length <> 6 Then
stk_Time = Format_String(stk_Time.Tri m, 6)
Public Shared Function Format_String_ToDate(stk_D ate As String) As String
If stk_Date.Trim <> "" Then
If stk_Date.Trim.Length <> 8 Then
stk_Date = Format_String(stk_Date.Tri m, 8)
End If
Dim yy As String = stk_Date.Substring(0, 4)
Dim mm As String = stk_Date.Substring(4, 2)
Dim dd As String = stk_Date.Substring(6, 2)
stk_Date = mm & "/" & dd & "/" & yy
Format_String_ToDate = stk_Date
Else
Format_String_ToDate = ""
End If
End Function
End If
stk_Time = stk_Time.Substring(0, 2) + ":" + stk_Time.Substring(2, 2) + ":" + stk_Time.Substring(4, 2)
Format_String_ToTime = stk_Time
Else
Format_String_ToTime = ""
End If
End Function
dgvStoreResult.Rows(i).Cel
Dim out_time As String = dgvStoreResult.Rows(i).Cel
dgvStoreResult.Rows(i).Cel
Public Shared Function Format_String_ToTime(stk_T
If stk_Time.Trim <> "" Then
If stk_Time.Trim.Length <> 6 Then
stk_Time = Format_String(stk_Time.Tri
Public Shared Function Format_String_ToDate(stk_D
If stk_Date.Trim <> "" Then
If stk_Date.Trim.Length <> 8 Then
stk_Date = Format_String(stk_Date.Tri
End If
Dim yy As String = stk_Date.Substring(0, 4)
Dim mm As String = stk_Date.Substring(4, 2)
Dim dd As String = stk_Date.Substring(6, 2)
stk_Date = mm & "/" & dd & "/" & yy
Format_String_ToDate = stk_Date
Else
Format_String_ToDate = ""
End If
End Function
End If
stk_Time = stk_Time.Substring(0, 2) + ":" + stk_Time.Substring(2, 2) + ":" + stk_Time.Substring(4, 2)
Format_String_ToTime = stk_Time
Else
Format_String_ToTime = ""
End If
End Function
ASKER
I've requested that this question be closed as follows:
Accepted answer: 0 points for RadhaKrishnaKiJaya's comment #a40474851
for the following reason:
I found the solution.
Accepted answer: 0 points for RadhaKrishnaKiJaya's comment #a40474851
for the following reason:
I found the solution.
I don't know if this is a follow-up question or a code review??? Here is what I did in my CellFormatting Event and how you would probably want to format it for reuse in your code:
Just modify colCreateDate and colCreateTime to be the Name of the columns in your datagrid for the Date and Time columns.
-saige-
Private Sub OnCellFormatting(ByVal sender As Object, ByVal e As DataGridViewCellFormattingEventArgs) Handles dgvStoreResult.CellFormatting
dgvStoreResult.Rows(e.RowIndex).ErrorText = String.Empty
If dgvStoreResult.Rows(e.RowIndex).IsNewRow Then Return
If dgvStoreResult.Columns(e.ColumnIndex).Name = "colCreateDate" Then
Dim tempDate As DateTime = DateTime.MinValue
If DateTime.TryParseExact(e.Value, "yyyyMMdd", CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, tempDate) Then
e.Value = tempDate.ToString("MM/dd/yyyy")
e.FormattingApplied = True
Else
dgvStoreResult.Rows(e.RowIndex).ErrorText = "Invalid date value for stock date."
e.FormattingApplied = False
End If
ElseIf dgvStoreResult.Columns(e.ColumnIndex).Name = "colCreateTime" Then
Dim tempTime As DateTime = DateTime.MinValue
If DateTime.TryParseExact(e.Value, "HHmmss", CultureInfo.InvariantCulture, Globalization.DateTimeStyles.None, tempTime) Then
e.Value = tempTime.ToString("HH:mm:ss")
e.FormattingApplied = True
Else
dgvStoreResult.Rows(e.RowIndex).ErrorText = "Invalid time value for stock time."
e.FormattingApplied = False
End If
End If
End Sub
Just modify colCreateDate and colCreateTime to be the Name of the columns in your datagrid for the Date and Time columns.
-saige-
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Form1.vb -
Open in new window
Form1.Designer.vb -
Open in new window
Products.xml -
Open in new window
Set products to always copy to output directory -
Produces the following output -
-saige-