数据表绑定到DataGrid - 前两列空不管是什么 [英] Binding DataTable to a DataGrid - first two columns empty no matter what

查看:101
本文介绍了数据表绑定到DataGrid - 前两列空不管是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一个DataTable绑定到一个DataGrid,然而,前两列总是空的,虽然它显然有内容。

例如,在数据表包含以下内容:

<$p$p><$c$c>╔═════════╦═════════╦═════════╦═════════╗
║║Columm1║列2║栏3║Column4
╠═════════╬═════════╬═════════╬═════════╣
║║A1 A2 A3║║║A4
║║B1 B2 B3║║║A4
║║C1 C2 C3║║║C4
╚═════════╩═════════╩═════════╩═════════╝

的含量可以通过行循环,它显示了正确的输出予以确认:

 的foreach(在Test.Rows的DataRow R)
{
    MessageBox.Show(R [0] +,+ R [1] +,+ R [2] +,+ R [3]);
}

但是,当我终于将DataTable绑定到DataGrid ...

  datagrid.DataContext = Test.DefaultView;

的前两列在的DataGrid 失踪的:

<$p$p><$c$c>╔═════════╦═════════╦═════════╦═════════╗
║║Columm1║列2║栏3║Column4
╠═════════╬═════════╬═════════╬═════════╣
║║║║A3 A4║
║║║║B3 A4║
║║║║C3 C4║
╚═════════╩═════════╩═════════╩═════════╝

我不知道为什么。 DataGrid中似乎是正确配置。

&LT; D​​ataGrid的X:名称=数据网格的Horizo​​ntalAlignment =左保证金=810,142,0,0
          VerticalAlignment =评出的HEIGHT =495WIDTH =400IsReadOnly =真
          的ItemsSource ={}绑定了borderThickness =0,1,1,1/&GT;


问题

列名称包含标点符号。消除这些解决这个问题。


解决方案

WPF DataGrid中有问题,是否有列名特殊字符或标点符号。如果删除这些字符,如预期它应该发挥作用。

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.

解决方案

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.

这篇关于数据表绑定到DataGrid - 前两列空不管是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆