2

I have a List of headers(columns) and then rows of data and want to show it in DataGrid with two way bindings

List<string> headers = new List<string> { "FirstName", "LastName", "Age" };
List<string> row1 = new List<string> { "John", "Doe", "19" };
List<string> row2 = new List<string> { "Jane", "Doe", "21" };
List<string> row3 = new List<string> { "Suzie", "Q", "52" };
List<string> row4 = new List<string> { "No", "Body", "48" };

List<List<string>> tableValues =
 new List<List<string>> { row1, row2, row3, row4 };

The editor does not let me show List of List since it has multiple <

I appreciate any help.

3
  • What do you have in XAML? What is your DataContext? Commented Apr 5, 2018 at 12:37
  • Why do you particularly want to have a list of list as your datasource. It seems unusual. Making the source a datatable seems a more obvious sort of choice and you can type the columns. Age is clearly going to be an integer. Commented Apr 5, 2018 at 13:11
  • I don't have a class representing the data It is coming from an xml column in DB. The headers can vary so does as rows. so I am representing the rows as list of list of strings Commented Apr 5, 2018 at 14:00

2 Answers 2

3

given that number of headers may vary, I suggest to tranform data in the format convenient for two-way data binding and use DataTable:

var dt = new DataTable();

// create columns and headers
int columnCount = headers.Count;
for (int i = 0; i < columnCount; i++)
    dt.Columns.Add(headers[i]);

// copy rows data
for (int i = 0; i < tableValues.Count; i++)
    dt.Rows.Add(tableValues[i].Take(columnCount).ToArray());

// display in a DataGrid
dataGrid.ItemsSource = dt.DefaultView;
Sign up to request clarification or add additional context in comments.

2 Comments

I modified my code and use a datatable in my code-behind. But it does not show the columns or rows. I have a UserControl for my xam. and just the datagrid <Grid> <DataGrid x:Name="myDataGrid" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" AutoGenerateColumns="False" > </DataGrid>
make AutoGenerateColumns="True"
1

Firstly create a class to hold your person information because how you are initializing your lists isn't good.

Public class Person
{
    public string Firstname {get; set;}
    public string Surname {get; set;}
    public int Age {get; set;}
}

Then you can create multiple people and store them in a list of type Person...e.g

//other code
List<Person> People = new List<Person>();
People.Add(new Person() { Firstname = "John", Surname = "Doe", Age = 19 });
//etc

Then in XAML all you have to do is point your Data grid at the People list and it should be able to bind to the various properties on each person or auto generate columns.

<DataGrid CanUserSortColumns="True" 
          CanUserAddRows="False"
          CanUserDeleteRows="False" 
          AutoGenerateColumns="False"
          ItemsSource="{Binding Path=People}"
          SelectionMode="Single">

    <DataGrid.Columns>
         <DataGridTextColumn     MinWidth="100"
                                         Width="Auto"
                                         IsReadOnly="False"
                                         Header="Firstname">
                                    <DataGridTextColumn.Binding>
                                        <Binding Path="Firstname"/>
                                    </DataGridTextColumn.Binding>
                  </DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

untested XAML but should give you a starting point

There are plenty of guides online that will be able to help with this instead of writing your own question on Stack Overflow.

3 Comments

I don't have a class representing the data It is coming from an xml column in DB. The headers can vary so does as rows. so I am representing the rows as list of list of strings
Then as Ash mentioned your best bet would be to dynamically create the data table other examples can be found here stackoverflow.com/questions/3991698/…
Thanks for your comments and help.

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.