dotnet / dotnet/wpf

DataTable Column Header containing "," symbol prevents cell rendering in WPF DataGrid

Open
#11,163 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

### Description

When binding a System.Data.DataTable to a WPF DataGrid, if the DataColumn.ColumnName contains a "," (Comma), all subsequent cells in that column fail to display values.

In the output, it throws the binding error as ,

```
System.Windows.Data Error: 40 : BindingExpression path error: '[]' property not found on 'object' ''DataRowView' (HashCode=20668333)'. BindingExpression:Path=[a,b_0]; DataItem='DataRowView' (HashCode=20668333); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
```

**Datatable codes:**

```
DataTable dataTable = new DataTable();
dataTable.Columns.Add("a,b", typeof(int));
dataTable.Columns.Add("cb", typeof(string));
dataTable.Columns.Add("Customer ID", typeof(string));
dataTable.Columns.Add("Country", typeof(string));

dataTable.Rows.Add(1001, "Maria Anders", "ALFKI", "Germany");
dataTable.Rows.Add(1002, "Ana Trujilo", "ANATR", "Mexico");
dataTable.Rows.Add(1003, "Antonio Moreno", "ENDGY", "Mexico");
```

**Column Declaration:**

```
private void FradataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
e.Column = new DataGridTextColumn { Header = e.Column.Header, Binding = new Binding("[" + e.Column.Header + "]") };
}
```

Sample Attached: [DataGrid_WPF.zip](https://github.com/user-attachments/files/22747404/DataGrid_WPF.zip)

### Reproduction Steps

1. Run the attached sample.
2. Observe the column "a,b0".
3. Check the output for the binding error.

### Expected behavior

The column "a,b0" display all values.

### Actual behavior

Empty cells under the column "a,b0".

### Regression?

_No response_

### Known Workarounds

_No response_

### Impact

_No response_

### Configuration

_No response_

### Other information

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.