otroumartastiK
otroumartastiK

Reputation: 21

Binding multiple list in Datagrid

I need to achieve a result like this:

enter image description here

xaml doesn't allow to create row via code, so I though to implement the row logic as a list. Each list is a row.

What I did essentially is this:

LastFiveHomeAwayMatches2 = new List<List<string>>()
                {
                  new List<string> {"Accrington", "D", "D", "W", "W", "W", "11" },
                  new List<string> { string.Empty, "1-1", "0-0", "0-1", "4-2", "0-3", string.Empty },
                  new List<string> {"Fos", "D", "D", "W", "W", "W", "11" },
                  new List<string> { string.Empty, "1-1", "0-0", "0-1", "4-2", "0-3", string.Empty },
                };

but how can I bind this multiple list in the Datagrid to achieve the result displayed? thanks. Update

<DataGrid AutoGenerateColumns="False" 
         ItemsSource="{Binding MatchController.LatestFiveMatches}">
            <DataGrid.Columns>
                   <DataGridTextColumn Header="Teams" />
                                    <DataGridTextColumn Header="5" />
                                    <DataGridTextColumn Header="4" />
                                    <DataGridTextColumn Header="3" />
                                    <DataGridTextColumn Header="2" />
                                    <DataGridTextColumn Header="1" />
                                    <DataGridTextColumn Header="Pt."/>
                                </DataGrid.Columns>

                            </DataGrid>

this doesn't display nothing. I need to display as header columns Teams, 5, 4, 3, 2, 1 also if the datagrid is blank.

Upvotes: 1

Views: 1367

Answers (1)

mm8
mm8

Reputation: 169160

Replace List<string> with a type that has a public property for each column you want to display, e.g.:

LastFiveHomeAwayMatches2 = new List<RowType>()
            {
              new RowType { Team = "Accrington", A = "D", B = "D", C = "W", D = "W", E = "W", F = "11" },
              ...
            };
dataGrid.ItemsSource = LastFiveHomeAwayMatches2;

public class RowType
{
    public string Team { get; set; }
    public string A { get; set; }
    public string B { get; set; }
    public string C { get; set; }
    public string D { get; set; }
    public string E { get; set; }
    public string F { get; set; }
}

Edit:

Also note that you must bind each column to the corresponding property:

<DataGridTextColumn Header="Teams" Binding="{Binding Team}" />

Upvotes: 2

Related Questions