0
votes

Task is trivial - populate WPF Datagrid control with content of DB table using old good ADO.NET tools. DataGrid has AutoGenerateColumns property set to True, so I don't specify Columns collection in XAML. Everything works fine but there is one snag - DataGRid Column header displays a name of table column incorrectly. For example, all coluns are named with convention like "Prefix_Word1_Word2_Word3". Instead of that, column name in DataGrid ignores first underline symbol, so I can see smth like "PrefixWord1_Word2_Word3". This behavior appears for multiple tables with same naming convention across multiple DBs.

Any suggestions about who's responsible for this?

Added piece of columns header of DataGrid

TPA is a prefix is this case. Instead of TPAID I supposed to see TPA_ID and so on.

1
Can you provide us some code? Or image of the result?Catarina Ferreira
@CatarinaFerreira it's not code-related issue, so I've added a screenshot of DataGrid ColumnsHeaderEugene

1 Answers

2
votes

WPF uses an underscore character instead of the ampersand character (like with WinForms) to prefix an access (a.k.a. accelerator or mnemonic) key in the text of its elements like Label and Button.

You can escape the underscore by using two underscores.

The underscore replaces the ampersand because, in XAML, ampersands can easily lead to mistakes and cause problems.

  • Solution 1

Just write an event handler like this to temporarily escape the underscores in the datagrid header.

private void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    string header = e.Column.Header.ToString();

    // Replace all underscores with two underscores, to prevent AccessKey handling
    e.Column.Header = header.Replace("_", "__");
}

You can find more help to this question in this next link: How to: Customize Auto-Generated Columns in the DataGrid Control