1

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.

2
  • it is the 4th time I see this question in a slightly different wording. sigh. consider using DataTable where rows are filled with your values (a simple example here: stackoverflow.com/a/44206066/1506454). Disable DataGrid sorting though, or the rows can be reordered otherwise Commented Sep 22, 2017 at 18:06
  • seems some stackoverflow users hate me, idk, I got banned for a question well explained, so no idea. Commented Sep 22, 2017 at 18:39

1 Answer 1

2

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}" />
Sign up to request clarification or add additional context in comments.

2 Comments

this is an elegant and simple solution, thank you. Just one question I need to display to the user the blank column as Teams, 5, 4, 3, 2, 1, Pt. I added an update to the question to show you my datagrid, anyway, not seems to display nothing.
See my edit. You need to bind each column to the corresponding property of the RowType class.

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.