|
1 | 1 | # How to bind data from the datatable to listview in Xamarin.Forms |
2 | 2 | This example demonstrates how to bind the data from the datatable to listview by converting the data table rows in to the collection. |
3 | 3 |
|
| 4 | +## Sample |
| 5 | + |
| 6 | +```xaml |
| 7 | +<Grid Margin="0"> |
| 8 | + <sync:SfListView x:Name="listView" |
| 9 | + ItemsSource="{Binding ContactsInfo}" |
| 10 | + ItemSize="70"> |
| 11 | + <sync:SfListView.ItemTemplate> |
| 12 | + <DataTemplate> |
| 13 | + <ViewCell> |
| 14 | + <ViewCell.View> |
| 15 | + <Grid x:Name="grid" RowSpacing="0"> |
| 16 | + <Grid.RowDefinitions> |
| 17 | + <RowDefinition Height="*" /> |
| 18 | + <RowDefinition Height="1" /> |
| 19 | + </Grid.RowDefinitions> |
| 20 | + . . . |
| 21 | + . . . |
| 22 | + <Label LineBreakMode="NoWrap" |
| 23 | + TextColor="#474747" VerticalOptions="CenterAndExpand" |
| 24 | + Text="{Binding ItemArray[0]}"> |
| 25 | + </Label> |
| 26 | + <Grid Grid.Column="1" |
| 27 | + RowSpacing="1" |
| 28 | + VerticalOptions="Center"> |
| 29 | + <Grid.RowDefinitions> |
| 30 | + <RowDefinition Height="*" /> |
| 31 | + <RowDefinition Height="*" /> |
| 32 | + </Grid.RowDefinitions> |
| 33 | + |
| 34 | + <Label LineBreakMode="NoWrap" |
| 35 | + TextColor="#474747" |
| 36 | + Text="{Binding ItemArray[1]}"> |
| 37 | + </Label> |
| 38 | + . . . |
| 39 | + . . . |
| 40 | + </Grid> |
| 41 | + </ViewCell.View> |
| 42 | + </ViewCell> |
| 43 | + </DataTemplate> |
| 44 | + </sync:SfListView.ItemTemplate> |
| 45 | + </sync:SfListView> |
| 46 | +</Grid> |
| 47 | + |
| 48 | +ViewModel.cs: |
| 49 | +public ObservableCollection<object> ContactsInfo |
| 50 | +{ |
| 51 | + get { return contactsInfo; } |
| 52 | + set { this.contactsInfo = value; } |
| 53 | +} |
| 54 | +public void GenerateSource(int count) |
| 55 | +{ |
| 56 | + contactsInfo = new ObservableCollection<object>(); |
| 57 | + dt = new DataTable("Student"); |
| 58 | + dt.Columns.Add("ContactID", typeof(Int32)); |
| 59 | + dt.Columns.Add("ContactName", typeof(string)); |
| 60 | + dt.Columns.Add("ContactType", typeof(string)); |
| 61 | + dt.Columns.Add("ContactNumber", typeof(string)); |
| 62 | + |
| 63 | + //Data |
| 64 | + for (int i = 0; i < count; i++) |
| 65 | + { |
| 66 | + dt.Rows.Add(i, CustomerNames[i], contactType[random.Next(0, 5)], random.Next(100, 400).ToString() + "-" + random.Next(500, 800).ToString() + "-" + random.Next(1000, 2000).ToString()); |
| 67 | + } |
| 68 | + |
| 69 | + for (int i = 0; i < dt.Rows.Count; i++) |
| 70 | + { |
| 71 | + contactsInfo.Add(dt.Rows[i]); |
| 72 | + } |
| 73 | +} |
| 74 | +``` |
| 75 | + |
4 | 76 | See [How to bind data from the datatable to listview in Xamarin.Forms](https://www.syncfusion.com/kb/9697/how-to-bind-data-from-the-dattable-to-listview-in-xamarin-forms) for more details. |
5 | 77 |
|
6 | 78 | ## <a name="requirements-to-run-the-demo"></a>Requirements to run the demo ## |
|
0 commit comments