I'm expecting, as Rohit Vats mentioned in his Comment too, that you have a wrong structure in your DataTable
.
var t = new DataTable();
// create column header
foreach ( string s in identifiders ) {
t.Columns.Add(new DataColumn(s)); // <<=== i'm expecting you don't have defined any DataColumns, haven't you?
}
// Add data to DataTable
for ( int lineNumber = identifierLineNumber; lineNumber < lineCount; lineNumber++ ) {
DataRow newRow = t.NewRow();
for ( int column = 0; column < identifierCount; column++ ) {
newRow[column] = fileContent.ElementAt(lineNumber)[column];
}
t.Rows.Add(newRow);
}
return t.DefaultView;
I have used this DataTable in a ValueConverter
and it works like a charm with the following binding.
<DataGrid AutoGenerateColumns="True" ItemsSource="{Binding Path=FileContent, Converter={StaticResource dataGridConverter}}" />
So what it does, the ValueConverter
transforms my bounded data (what ever it is, in my case it's a List<string[]>
) into a DataTable
, as the code above shows, and passes this DataTable
to the DataGrid
. With specified data columns the data grid can generate the needed columns and visualize them.
To say it in a nutshell, in my case the binding to a DataTable
works like a charm.