casillas
casillas

Reputation: 16793

Population of TableView Data in Xamarin

I have successfully fetched data from the URL and assigned it to my Customers dictionary and and I could able to write each customer object on my output. However, since I am quite new on this platform, I could not able to populate the data on TableView Cell. I have attached my code.

private IEnumerable <IDictionary<string,object>> Customers;
public MyDataServices ()
        {
            InitializeComponent ();
            InitializeDataService ();
            GetDataFromOdataService ();

            foreach (var customers in Customers){
                System.Diagnostics.Debug.WriteLine((string)customers["ContactName"]);
                // populate Data on TableView

            }

            Padding = new Thickness (10, 20, 10, 10);
            Content = new StackLayout () {
                Children = { tableView }

            };
}

Upvotes: 0

Views: 562

Answers (1)

rdavisau
rdavisau

Reputation: 895

Assuming customer instances have ContactName and ContactPosition fields, this creates basic TextCells with those details and adds them to the table view.

// create a TableSection to hold the cells
var section = new TableSection("Customers");

foreach (var customer in Customers){
    System.Diagnostics.Debug.WriteLine((string)customers["ContactName"]);
    // populate Data on TableView
    var name = (string)customer["ContactName"];
    var position = (string)customer["ContactPosition"]

    var cell = new TextCell { Text = name, Detail = position };
    section.Add(cell);
}

// add the section to the TableView root
tableView.Root.Add(section);

You then add the TableView to your layout as normal.

There's information in the Xamarin docs here

Upvotes: 1

Related Questions