How to know the type of the data in the CellFormatting event

2 Answers 71 Views
ListView
ASM
Top achievements
Rank 1
Iron
Iron
ASM asked on 22 Mar 2023, 11:31 AM

Hi,

I'm using a ListView in detail view mode and was trying to format all date values so that they are displayed in short date format.
It looks like the correct way to do this is within the CellFormatting event, so it would be something like this:

Function CellFormatting(sender As Object, e As ListViewCellFormattingEventArgs)
	If isDate Then
		e.CellElement.Text = String.Format(strFormat, DirectCast(e.CellElement, DetailListViewDataCellElement).Row(e.CellElement.Data))

The thing is, I'm not sure how to determine the data type or column type that corresponds to this e.CellElement. How can I tell if the current cell contains a date, or a int, or a string, or whatever?

Thanks!

2 Answers, 1 is accepted

Sort by
0
Dinko | Tech Support Engineer
Telerik team
answered on 23 Mar 2023, 09:17 AM

Hello Armand SM,

The Data property of the e.CellElement gives you information about the column. You can use the FieldName property to get the name of the property to which the column is pointing to.

Private Sub ListViewCellFormatting(sender As Object, e As ListViewCellFormattingEventArgs)
    Dim column = e.CellElement.Data
    Dim text = column.FieldName
End Sub

Regards,
Dinko | Tech Support Engineer
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
ASM
Top achievements
Rank 1
Iron
Iron
answered on 23 Mar 2023, 10:52 AM

Hi Dinko,

Yes, but I was trying to do it in a more generic way, not by field name, but by data type.

I mean, I could do the following:

Dim isDate As Boolean = DateTime.TryParse(DirectCast(e.CellElement, DetailListViewDataCellElement).Row(e.CellElement.Data), Nothing)

But I just think there must be a more practical way to do this. 

If I'm not wrong, I'd say this was possible in RadGridView. Maybe it's just not in RadListView, I don't know, but it seems quite unlikely to me, the data type should be somewhere in there, shouldn't it? Aren't these cells created according to the type of data they contain?

Thanks!

Dinko | Tech Support Engineer
Telerik team
commented on 23 Mar 2023, 12:00 PM

The column in RadListView does not expose public API to get the type of the underlying bound property. After searching for a way to get the type of column I think I was able to find one. In my case, the RadListView is bound to a DataTable. In the CellFormatting event, you can get the DataBoundItem of the Row and try to match the DataTable column name with the e.CellElement.Data.FieldName property. Check the following code snippet:

Private Sub ListViewCellFormatting(sender As Object, e As ListViewCellFormattingEventArgs)
    Dim column = e.CellElement.GetPropertyValue(DetailListViewCellElement.CurrentProperty).StyleVersion.GetType()
    Dim dataBoundItem = DirectCast(e.CellElement, Telerik.WinControls.UI.DetailListViewDataCellElement).Row.DataBoundItem
    Dim columns = DirectCast(dataBoundItem, DataRowView).Row.Table.Columns

    Dim queryResults = From col In columns
                        Where DirectCast(col, DataColumn).ColumnName = e.CellElement.Data.FieldName
                        Select DirectCast(col, DataColumn).DataType
    Debug.WriteLine(queryResults(0))
End Sub

ASM
Top achievements
Rank 1
Iron
Iron
commented on 23 Mar 2023, 02:11 PM

Fine, I'll go that way, it's not exactly what I had in mind, but it might still work for me.

Thanks for the suggestion!

Tags
ListView
Asked by
ASM
Top achievements
Rank 1
Iron
Iron
Answers by
Dinko | Tech Support Engineer
Telerik team
ASM
Top achievements
Rank 1
Iron
Iron
Share this question
or