cancel
Showing results for 
Search instead for 
Did you mean: 

Change date format in table?

Liesbeth
Explorer
0 Kudos

Hi all,

I'm trying to make my first VB.NET iview and I need some help! I can already display the results of a RFC in a databound table, but now the dates are displayed in YYYMMDD format. How can I change this to a .NET date (DD/MM/YYYY)?

Thanks!

Liesbeth

Accepted Solutions (1)

Accepted Solutions (1)

reiner_hille-doering
Active Contributor
0 Kudos

What you want to do is a kind of fomating the data in the cells. One possible way is to subscribe to the ItemDatabound event of the Table and format the contents of the cell accordingly:

Private Function ReformatDate(ByVal format As String)

Return format.Substring(6, 2) + "/" + format.Substring(4, 2) + "/" + format.Substring(0, 4)

End Function

Private Sub Table1_ItemDatabound(ByVal sender As Object, ByVal e As SAP.Web.UI.Controls.Table.ItemEventArgs) Handles Table1.ItemDatabound

Dim dateView As TextView = CType(e.Item.Cells(DateColumnIndex).TableCellContent, TextView)

dateView.Text = ReformatDate(dateView.Text)

End Sub

DateColumnIndex is the index of the column that has the date field.

There is an even easier tricky way to do it: If you databind the table at design-time, the framwork creates databinding expressions for the TextViews in the cells that look like

Text='<%# DataBinder.Eval(Container.Parent, "DataSourceRow.DataItem.Fldate") %>'

You can change this to contain your formatting function, e.g.

Text='<%# ReformatDate(CType(DataBinder.Eval(Container.Parent, "DataSourceRow.DataItem.Fldate"), string) %>'

You can change the expression either in HTML view or usign the DataBindings property editor of the cell's TextView. In this case of cause you don't need the Table1_ItemDatabound event handler.

Liesbeth
Explorer
0 Kudos

Thanks Reiner for your quick answer! Both solutions work just fine! I've used the RfcConvert.RfcDateToDateTime function to convert the RFC date type to a .NET DateTime instead of a creating my own formatting function.

Private Sub Table1_ItemDatabound(ByVal sender As Object, ByVal e As SAP.Web.UI.Controls.Table.ItemEventArgs) Handles Table1.ItemDatabound

Dim dateView As TextView = CType(e.Item.Cells(DateColumnIndex).TableCellContent, TextView)

dateView.Text = CStr(RfcConvert.RfcDateToDateTime(dateView.Text))

End Sub

Liesbeth

Answers (1)

Answers (1)

Former Member
0 Kudos

right click on the table, go to Edit Columns, choose the right Column in the members list on the left.

Open the TableCellContent on the right, open the DataBinding menu, choose the text property on the left, and choose the Custom binding expression on the right.

You will see something like

"DataBinder.Eval(Container.Parent, "DataSourceRow.DataItem.date")".

This is the string that represents the date.

Just write your own converting method, or use one from the framework, lets say ConvertToMyFormat(string date)

and edit the custom binding expression as follows:

ConvertToMyFormat(DataBinder.Eval(Container.Parent, "DataSourceRow.DataItem.date")),

that is the default binding expression in the parameter sent to your method.

And you are done. You could see the changes in the Html view.

Hope it helps

Liesbeth
Explorer
0 Kudos

Thanks Tsachi for your answer. A newbe like me can use this kind of help!

Liesbeth