无法将 WPF DataGrid 绑定到列名称包含空格和其他特殊字符的表

发布于 2024-12-31 22:54:11 字数 885 浏览 0 评论 0原文

当我有一个简单的 DataSet ,其中有一个带有简单列名(即没有空格或句点)的表时,如下代码就可以正常工作:

  DataGrid resultsGrid=...; // Actually defined in the XAML
  DataSet ds=...; // The is the DataSet that contains one table
  Binding binding = new Binding();

  resultsGrid.DataContext=ds.Tables[0];
  resultsGrid.SetBinding(DataGrid.ItemsSourceProperty, binding);

上面示例中的 DataGrid 具有 AutoGenerateColumns 属性设置为 True 并正确填充 DataSet 中表中的数据。

但是,如果我的表中的列的名称包含空格/句点或其他特殊字符,则自动绑定似乎会失败。我收到如下错误:

System.Windows.Data 信息:20:BindingExpression 不能 由于信息缺失而检索价值。绑定表达式:Path=My 上校姓名; DataItem='DataRowView' (HashCode=8146871);目标元素 是“TextBlock”(名称=“”);目标属性是“文本”(类型“字符串”)

显然,自动生成的绑定表达式 Path=My Col. Name 无效。整个路径需要“引用”以允许空格和句点。对于具有更复杂列名的表,有什么方法可以坚持使用 AutoGenerateColumns,还是我现在必须全部手动操作?

When I have a simple DataSet that has a table with simple column names (i.e., no spaces or periods), code such as the following works just fine:

  DataGrid resultsGrid=...; // Actually defined in the XAML
  DataSet ds=...; // The is the DataSet that contains one table
  Binding binding = new Binding();

  resultsGrid.DataContext=ds.Tables[0];
  resultsGrid.SetBinding(DataGrid.ItemsSourceProperty, binding);

The DataGrid in the above example has the AutoGenerateColumns attribute set to True and properly populates its data from the table in the DataSet.

However, if my table has columns with names containing spaces / periods or other special characters, the automated binding seems to fail. I get errors such as:

System.Windows.Data Information: 20 : BindingExpression cannot
retrieve value due to missing information. BindingExpression:Path=My
Col. Name; DataItem='DataRowView' (HashCode=8146871); target element
is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

Clearly, the auto-generated binding expression Path=My Col. Name is not valid. The entire path needs to be "quoted" so as to allow spaces and periods. Is there any way to stick with AutoGeneratedColumns for tables with more complex column names, or do I have to go all manual now?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌伤浅笑 2025-01-07 22:54:11

您可以尝试处理 resultsGrid.AutoGenerateColumns - 或者更佳的是,在集合进行数据绑定之前仅对网格执行此操作 - 以替换或删除列名称中的空格或句点。不过,您必须对网格和数据集执行此操作,因此,如果您的数据被编辑和保存,则此方法可能会比它的价值更麻烦。其效率和可行性在很大程度上取决于您的应用程序的结构和行为,而且我还没有对其进行测试,因此请对此持保留态度。

据我发现(诚然这不是一个非常广泛的领域),你几乎陷入了这种行为。我建议向 Microsoft 提交错误;看来这对于 AutoGenerateColumns 来说是理想的行为。

You could try handling resultsGrid.AutoGeneratingColumns - or more preferably, do this just to the grid before the set gets databound - to replace or remove spaces or periods in the column names. You'd have to do this to both the grid and DataSet, though, so if your data gets edited and saved, this method would likely be far more hassle than it's worth. The efficiency and feasibility of this depends heavily on your app's structure and behavior, plus I have not tested it, so take it with a sizable grain of salt.

As far as I have found (which admittedly isn't a terribly broad domain), you're pretty much stuck with this behavior. I'd suggest submitting a bug to Microsoft; it seems like this would be desirable behavior for AutoGenerateColumns.

白况 2025-01-07 22:54:11

您可以将 SQL select 格式化为不包含空格

 select [Column A] as 'Column_A' from table

You could format the SQL select to have no spaces

 select [Column A] as 'Column_A' from table
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文