0

I have a big issue regarding the output in a DataGrid in a tool that outputs some calculated values. As part of the program I have a DataGrid:

<DataGrid AutoGenerateColumns="True" EnableRowVirtualization="True" EnableColumnVirtualization="True" Margin="6,14,6,6" Name="valueGrid" ItemsSource="{Binding}" MaxColumnWidth="70" MinColumnWidth="70" ColumnHeaderHeight="23" IsReadOnly="True" CanUserSortColumns="False" CanUserReorderColumns="False" />

Then I add dynamically columns to it

public partial class MainWindow : Window
{

    private DataView _dv;

private void BuildGrid(int columns, int rows, double[,] values, double x, double deltax)
    {
        DataTable dt = new DataTable();
        int i, j;
        string pos;

        DataColumn cl = null;

        for (i = 0; i < columns; i++)
        {
            pos = string.Format("{0:0.0000}", x + i * deltax);
            cl = new DataColumn("x = " + pos);
            dt.Columns.Add(cl);
        }

        _dv = new DataView(dt);
        valueGrid.ItemsSource = _dv;

        for (i = 0; i < rows; i++)
        {
            DataRowView rw = _dv.AddNew();

            j = 0;

            foreach (DataColumn col in _dv.Table.Columns)
            {
                rw[col.ColumnName] = string.Format("{0:0.000000}", values[i, j]);
                j++;
            }
        }
    }
}

Now I am running a German OS (so the decimal seperator is a comma) but study in the UK so I wanted to change my CultureInfo to English, which would replace the comma with a dot, everything works fine but there seems to be an issue regarding the dot in the name of the columns and all cells of the DataGrid remain empty after the change. I also tried it adding the string ".test" to the end of the each column name instead of changing the language and the same problem arose. I get the following error message:

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

Same message for every single column just with a different x. Using the .test version and again the German language (so the comma instead of dot as decimal separator) I get:

System.Windows.Data Error: 40 : BindingExpression path error: 'x = 8,0000' property not found on 'object' ''DataRowView' (HashCode=25662852)'. BindingExpression:Path=x = 8,0000.test; DataItem='DataRowView' (HashCode=25662852); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

Does anyone see a solution to this problem?

Regards, Phil

H.B.
  • 166,899
  • 29
  • 327
  • 400
philkark
  • 2,417
  • 7
  • 38
  • 59
  • I would try AutoGenerateColumns= False and see if you can manually create a column with a . in the name. If so then I would bind to List> as DataTable has a lot of overhead. If you are not editing then you could try GridView ListView. I just tested and you can have a . in the Header of a a GridViewColumn. – paparazzo Mar 18 '12 at 14:12

1 Answers1

1

DataColumn.ColumnName follows the same rules as column names do in MS SQL. The stack article on acceptable column names in MS SQL that covers that is here:

I was also able to find a training lesson on that here, but not the original MS source on the topic.

In short, "." is not an acceptable character for a column name.

Community
  • 1
  • 1
Xcalibur37
  • 2,305
  • 1
  • 17
  • 20
  • Thank's for the answer, I guess I will just think of a different solution to specify the postion... – philkark Mar 18 '12 at 16:54