Louitabitbol
Louitabitbol

Reputation: 21

WPF Datagrid ItemsSource as datatable with columns containing binding syntax

I created a window which displays a datagrid. The items source of this datagrid is a datatable, created dynamically (i.e. may contain a various number of columns).

It works fine in most cases, however, when the header of a column contains a binding syntax character, the binding fails, the column is empty and I see an error in the output console.

Example:

<Window x:Class="WpfApplication6.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication6"
        mc:Ignorable="d"
        SizeToContent="WidthAndHeight"
        Title="MainWindow">
    <Grid>
        <DataGrid Name="dgvGraphData"
                  AutoGenerateColumns="True"
                  IsReadOnly="True"
                  AlternatingRowBackground="Gainsboro"
                  BorderThickness="0"
                  SelectionMode="Single"
                  ItemsSource="{Binding Path=GraphData}" />
    </Grid>
</Window>

Then in the code behind, I do this

public partial class MainWindow : Window
{
    public DataTable GraphData { get; set; }

    public MainWindow()
    {
        InitializeComponent();
        GraphData = GetDataTable();
        DataContext = this;
    }

    DataTable GetDataTable()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("column name")); //appears fine
        dt.Columns.Add(new DataColumn("column /name"));//binding fails
        dt.AcceptChanges();
        DataRow dr = dt.NewRow();
        dr[0] = "foo";
        dr[1] = "bar";
        dt.Rows.Add(dr);
        dt.AcceptChanges();
        return dt;
    }
}

I get the following error message in the output console:

System.Windows.Data Error: 40 : BindingExpression path error: 'column' property not found on 'object' ''DataRowView' (HashCode=2754490)'.
BindingExpression:Path=column /name; 
DataItem='DataRowView' (HashCode=2754490); target element is 'TextBlock' (Name=''); 
target property is 'Text' (type 'String')

I guess I need to escape the character but I can't seem to find the right syntax. The only workaround I found so far is, when populating my datatable, replacing the "problematic" chars with something else, it works as the binding succeeds, but the end result is confusing for the user as the header text may differ (missing problematic characters).

Is there any way I can solve this?

Upvotes: 0

Views: 1539

Answers (1)

Louitabitbol
Louitabitbol

Reputation: 21

Ok, found the solution here, thanks to Magnus:

<DataGrid Name="dataGrid" AutoGenerateColumns="True" AutoGeneratingColumn="dataGrid_AutoGeneratingColumn" />

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

Upvotes: 2

Related Questions