1

I have the following ListView-

<ListView x:Name="listViewm" ItemsSource="{ Binding Rows }">
        <ListView.Header >
            <Grid >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="1*"></ColumnDefinition>
                    <ColumnDefinition Width="1*"></ColumnDefinition>
                    <ColumnDefinition Width="1*"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Label Text="Name" Grid.Column="3"  FontSize="Medium" />
                <Label Text="Age" Grid.Column="2" FontSize="Medium" />
                <Label Text="DOB" Grid.Column="1" FontSize="Medium" />
            </Grid>
        </ListView.Header>

        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Grid >
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="1*"></ColumnDefinition>
                            <ColumnDefinition Width="1*"></ColumnDefinition>
                            <ColumnDefinition Width="1*"></ColumnDefinition>
                        </Grid.ColumnDefinitions>

                        <!--I need my Row values here-->

                    </Grid>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

I need to create a row for each entry I have in a list and add it to the grid dynamically -

protected override void OnAppearing()
{
    CreateGrid();
}

public ObservableCollection<Row> Rows { get; set; }

public class Row
{
    public string Name { get; set; }
    public string Age { get; set; }
    public string DOB { get; set; }
}

public void CreateGrid()
{
    Rows = new ObservableCollection<Row>();
    Rows.Clear();

    foreach(var entry in entryList) { // entryList just contains values I use to populate row info 

        var row = new Row();
        row.Name = entry.name;
        row.Age = entry.age;
        row.DOB = entry.dob;

        Rows.Add(row);
    }
}

The itemSource is not binding so no new rows are being create, I tried removing ItemsSource="{ Binding Rows }" in the XAML and using listViewm.ItemsSource = Rows in the code behind instead however this also did not work.

So if entryList had 3 test entries for John, Bob and Bill the table should look like -

+----------------+
| Name  Age DOB  |
+----------------+
| John  31 06/09 |
| Bill  32 07/10 |
| Bob   34 08/11 |
+----------------+

2 Answers 2

2

Your are not adding your values to the listview.

<ListView x:Name="listViewm" ItemsSource="{ Binding Rows }">
    <ListView.Header >
        <Grid >
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*"></ColumnDefinition>
                <ColumnDefinition Width="1*"></ColumnDefinition>
                <ColumnDefinition Width="1*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Label Text="Name" Grid.Column="3"  FontSize="Medium" />
            <Label Text="Age" Grid.Column="2" FontSize="Medium" />
            <Label Text="DOB" Grid.Column="1" FontSize="Medium" />
        </Grid>
    </ListView.Header>

    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <Grid >
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="1*"></ColumnDefinition>
                        <ColumnDefinition Width="1*"></ColumnDefinition>
                        <ColumnDefinition Width="1*"></ColumnDefinition>
                    </Grid.ColumnDefinitions>

                    <!--I need my Row values here-->
                    <Label Text="{Binding Name}" />
                    <Label Grid.Column="1" Text="{Binding Age}" />
                    <Label Grid.Column="2" Text="{Binding DOB}" />

                </Grid>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>

Sign up to request clarification or add additional context in comments.

4 Comments

Still no values :(
How are you retrieving the data of your your entryList ? Make sure the entryList is not null or empty.
The entry list has values, I even just created a new row manually and added it to Rows collections, still nothing :( I am new to mobile development but perhaps this is something to do with a view model?
Your entryList should be public not private. Can you show how you declare it ?
1

I could be wrong but when you initialize the Rows object , you should update the itemsSource of the listview.

public void CreateGrid()
{
    Rows = new ObservableCollection<Row>();
    Rows.Clear();
    // Rows is initialized as a new collection so update the itemssource
    listViewm.ItemsSource = Rows;

    foreach(var entry in entryList) 
    { 
        // entryList just contains values I use to populate row info 
        var row = new Row();
        row.Name = entry.name;
        row.Age = entry.age;
        row.DOB = entry.dob;

        Rows.Add(row);
    }
}

1 Comment

It might even be better to initialize the Rows component in the screen class constructor instead of the CreateGrid function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.