listview.datasource to a datatable

Hi,

Iam using syncfusion xamarin forms listview. I'am a Visual Basic Programmer. Iam using portable visualbasic class to run xamarin application. I tried all of your controls and are working perfect in vb portable class.

I used your sample code provided here (https://help.syncfusion.com/xamarin/listview/getting-started#defining-an-itemtemplate) and works perfect..(converted to portable vb and works fine with BookInfoRepository viewModel)

My requirement is to use a datatable to set the listview.datasource. I found a solution here( Bind data from the DataTable to SfListView | Xamarin - SfListView (syncfusion.com).

But Iam getting blank rows rendered without text instead of ObservableCollection data. Am I Missing something?

My vb portable code is as below



Imports Xamarin.Forms

Imports Syncfusion.XForms.Buttons

Imports Syncfusion.XForms.PopupLayout

Imports Syncfusion.XForms.Cards

Imports Syncfusion.SfDataGrid.XForms

Imports System.Net

Imports System.IO

Imports Newtonsoft.Json

Imports System.Data

Imports System.Timers

Imports Syncfusion.ListView.XForms

Imports System.Collections.ObjectModel

Imports System.Dynamic

Imports System.Runtime.CompilerServices


Public Class App

Inherits Application

Dim l As Integer = 0

Dim listView As SfListView

Dim contactsInfo As New ObservableCollection(Of Object)

Dim dt As DataTable


Public Function fetch_data() As DataTable

Dim tmpURL As String = "https://ej2services.syncfusion.com/production/web-services/api/Orders"

Dim theWebRequest = HttpWebRequest.Create(tmpURL)

theWebRequest.Headers.Add(HttpRequestHeader.Pragma, "no-cache")

Dim theWebResponse = CType(theWebRequest.GetResponse(), HttpWebResponse)

Dim theResponseStream = New StreamReader(theWebResponse.GetResponseStream())

Dim result As String = theResponseStream.ReadToEnd()

Dim l As DataTable = JsonConvert.DeserializeObject(Of DataTable)(result)

Return l

End Function


Public Sub New()

Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense(Syncfusion_Xamarin_License)

listView = New SfListView

dt = fetch_data ()

With listView

contactsInfo = New ObservableCollection(Of Object)()

For i As Integer = 0 To dt.Rows.Count - 1

contactsInfo.Add(dt.Rows(i))

Next

.ItemsSource = contactsInfo

.ItemSize = 100

End With



listView.ItemTemplate = New DataTemplate(Function()

Dim Grid = New Grid()

Dim bookName = New Label With {.FontAttributes = FontAttributes.Bold, .TextColor = Color.White, .BackgroundColor = Color.Teal, .FontSize = 21}

bookName.SetBinding(Label.FormattedTextProperty, New Binding("ShipCity"))

Dim bookDescription = New Label With {.FontSize = 15}

bookDescription.SetBinding(Label.TextProperty, New Binding("ShipCity"))

Grid.Children.Add(bookName)

Grid.Children.Add(bookDescription, 1, 0)

Return Grid

End Function)

MainPage = New ContentPage With {.Content = listView}

End Sub


End Class



3 Replies

LN Lakshmi Natarajan Syncfusion Team July 28, 2021 08:14 AM UTC

Hi Shahim, 
 
Thank you for using Syncfusion products. 
 
We have checked the reported query “listview.datasource to a datatable using VB” from our side. We would like to inform you that when getting data from the DataTable, the contactsInfo collection has the items type of DataRow. So, we need to bind the Column name to the Label to display its value. Please refer to the following data which added to the contactsInfo collection.  
 
 
 
 
You can get the Properties name (Column name) using index. Please refer to the following code snippets to achieve your requirement, 
listView.ItemTemplate = New DataTemplate(Function() 
 
                                                Dim Grid = New Grid() 
 
                                                Dim bookName = New Label With {.FontAttributes = FontAttributes.Bold, .FontSize = 21} 
 
                                                bookName.SetBinding(Label.TextProperty, New Binding("ItemArray[0]")) // OrderId 
 
                                                Dim bookDescription = New Label With {.FontSize = 15} 
 
                                                bookDescription.SetBinding(Label.TextProperty, New Binding("ItemArray[4]")) //ShipCity 
 
                                                Grid.Children.Add(bookName) 
 
                                                Grid.Children.Add(bookDescription, 1, 0) 
 
                                                Return Grid 
 
                                            End Function) 
 
 
Please refer to our tested sample in the following link, 
 
Please let us know if you need further assistance. 
 
Lakshmi Natarajan 
  
 



SH shahim July 28, 2021 12:23 PM UTC

Thank you Lakshmi,

Its working fine now.



LN Lakshmi Natarajan Syncfusion Team July 28, 2021 12:30 PM UTC

Hi Shahim, 
 
Thank you for the update. 
 
We are glad that your requirement has been met at your end. Please let us know if you need any further update. As always we are happy to help you out. 
 
Lakshmi Natarajan 
 


Loader.
Up arrow icon