jacobz
jacobz

Reputation: 3291

Binding DataTable to a DataGrid - first two columns empty no matter what

I'm binding a DataTable to a DataGrid, however, the first two columns are always empty, although it clearly has content.

For example, the DataTable contains following:

╔═════════╦═════════╦═════════╦═════════╗
║ Columm1 ║ Column2 ║ Column3 ║ Column4 ║
╠═════════╬═════════╬═════════╬═════════╣
║ A1      ║ A2      ║ A3      ║ A4      ║
║ B1      ║ B2      ║ B3      ║ A4      ║
║ C1      ║ C2      ║ C3      ║ C4      ║
╚═════════╩═════════╩═════════╩═════════╝

The content can be confirmed by looping through the rows, which displays the correct output:

foreach (DataRow r in Test.Rows)
{
    MessageBox.Show(r[0] + ", " + r[1] + ", " + r[2] + ", " +  r[3]);
}

But when, I finally bind the DataTable to the DataGrid...

datagrid.DataContext = Test.DefaultView;

The first two columns of the DataGrid go missing:

╔═════════╦═════════╦═════════╦═════════╗
║ Columm1 ║ Column2 ║ Column3 ║ Column4 ║
╠═════════╬═════════╬═════════╬═════════╣
║         ║         ║ A3      ║ A4      ║
║         ║         ║ B3      ║ A4      ║
║         ║         ║ C3      ║ C4      ║
╚═════════╩═════════╩═════════╩═════════╝

I have no clue why. The DataGrid seems to be configured correctly.

<DataGrid x:Name="datagrid" HorizontalAlignment="Left" Margin="810,142,0,0"
          VerticalAlignment="Top" Height="495" Width="400" IsReadOnly="True"
          ItemsSource="{Binding}" BorderThickness="0,1,1,1"/>

Problem

The column names contained punctuation. Removing those solved the issue.

Upvotes: 3

Views: 614

Answers (1)

Chanicho
Chanicho

Reputation: 354

wpf Datagrids have issues if there are special characters or punctuation in the column names. If you remove those characters, it should function as expected.

Upvotes: 2

Related Questions