1
votes

I have a DBF file read into a datatable and display with a datagrid in WPF. This DBF file is special which has a column name "A.B". After binding this column, the binding expressing might trade "A" as a property of the datarowview, wile "B" is "A"'s property. Is there a way to workaround this issue?

Here is the code to recreate this issue. Just have a DataGrid names "dg" in the XAML. When press F5, it should display "Godspeed" in the first row, but nothing appears.

DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(int));
            dt.Columns.Add("A.B");

            DataRow row = dt.NewRow();
            row[0] = 1;
            row[1] = "Godspeed";
            dt.Rows.Add(row);

            DataGridTextColumn colA = new DataGridTextColumn();
            colA = new DataGridTextColumn();
            colA.Binding = new Binding("A.B");
            colA.Header = "A.B";
            dg.Columns.Add(colA);

            var dv = new DataView(
                dt, 
                "", 
                "ID ASC",  
                DataViewRowState.CurrentRows);

            dg.ItemsSource = dv;

Here is a screenshot of the effect of my code: enter image description here

And here is the expression exception watched by snoop. So I'm sure the issue is caused by the dot of "A.B" in the binding path.

System.Windows.Data Error: 40 : BindingExpression path error: 'A' property not found on 'object' ''DataRowView' (HashCode=60213203)'. BindingExpression:Path=A.B; DataItem='DataRowView' (HashCode=60213203); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') Thanks,

2
Thanks for your help; I'm still struggle on this issue. We are in X64 environment; so that we cannot use the OleDbProvider etc; so we read DBF with binary reader alike. The code above is a simple sample to recreate this issue, I'm not sure there is any different read from OleDb or our method, the column type are the same, the value is the same in the data source. So I think the core issue is from the binding expression. Please see my edited question. - Howard
Strange. Hm..have you tried escaping it e.g. "A\.B"? - KMC
"A\.B" is illegal to use. But I tried using [A.B] fixed the problem; but in this way, when clicking the column header to sort, it throws an exception about the SortMemberPath, so we need hook an event to trim the '[' and ']' to make it work. Thanks anyway. - Howard
opps. didn't realize you got the square bracket already down. yes i still have the sorting exception. so answer's not completed yet.. - KMC
No problem, thanks for your help anyway:) - Howard

2 Answers

2
votes

Plus the answer from KMC, you might have the sorting exception. Please hook "Sorting" event on the DataGrid and trim the '[' and ']' of e.Column.SortMemberPan will fix this issue.

2
votes

Use square bracket.

DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("A.B");

DataRow row = dt.NewRow();
row[0] = 1;
row[1] = "Godspeed";
dt.Rows.Add(row);

dg.AutoGenerateColumns = false;
DataGridTextColumn colA = new DataGridTextColumn();
DataGridTextColumn colB = new DataGridTextColumn();
colA = new DataGridTextColumn();
colB = new DataGridTextColumn();
colA.Binding = new Binding("ID");
colB.Binding = new Binding("A.B");
colA.Header = "ID";
colB.Header = "A.B";
dg.Columns.Add(colA);
dg.Columns.Add(colB);

var dv = new DataView(
    dt,
    "",
    "ID ASC",
    DataViewRowState.CurrentRows);

dg.ItemsSource = dv;

But sorting throws exception