Reputation: 2712
I have to List of Data :
List<string[]> dataRow = new List<string[]>();
I'm trying to display it like this :
XAML :
<DataGrid Margin="8,259,8,8" IsReadOnly="True" AutoGenerateColumns="False" AlternatingRowBackground="Gainsboro" AlternationCount="2" Name="MyDataGrid" />
C# :
MyDataGrid.ItemsSource = dataRow;
foreach( string[] cellContent in dataRow )
{
foreach( string text in cellContent )
{
var column = new DataGridTextColumn
{
Binding = new Binding(text)
};
MyDataGrid.Columns.Add(column);
}
}
but i got a "ContextSwitchDeadlock" Error. How can i solve this?
Thank u for Helping
EDIT : solved but not with the databinding Way
I solved it like this :
List<string> ColumnName = new List<string>();
List<string[]> dataRow = new List<string[]>();
DataTable myTable = new DataTable();
// Fill Array ColumnName and dataRow Here
foreach (string text in ColumnName)
{
myTable.Columns.Add(text);
}
foreach (string[] cellContent in dataRow)
{
myTable.Rows.Add(cellContent);
}
DatensatzGrid.ItemsSource = myTable.AsDataView();
Thank u for all the reply!!
Upvotes: 3
Views: 3277
Reputation: 84657
The DataGrid
control doesn't support Binding to 2D arrays, List<List..>>
etc.
See this question: How to populate a WPF grid based on a 2-dimensional array
I created a subclassed DataGrid
(DataGrid2D) to achieve this a while back.
To use it just add a reference to DataGrid2DLibrary.dll, add this namespace
xmlns:dg2d="clr-namespace:DataGrid2DLibrary;assembly=DataGrid2DLibrary"
and then create a DataGrid2D and bind it to your IList, 2D array or 1D array like this
<dg2d:DataGrid2D Name="MyDataGrid"
ItemsSource2D="{Binding DataRow}"/>
Since dataRow is a field a not a property binding won't work, but just setting the ItemsSource2D
in code behind will be enough to display it
MyDataGrid.ItemsSource2D = dataRow;
Upvotes: 4