0
votes

I am having some problems when I have a datatable and I want to bind each datagridtextcolumn in the datagrid to each column in the datatable.

my code is as below

public class Packet
{
    public Header header { get; set; }
    public Frame frame { get; set; }
    public Tail tail { get; set; }
    public String id { get; set; }

    public Packet(String id,Header header, Frame frame, Tail tail)
    {
        this.id = id;
        this.header = header;
        this.frame = frame;
        this.tail = tail;
    }

}

Hence on my xaml side

<Grid >
    <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Path=.}" Name="dgrid" SelectionChanged="dgrid_SelectionChanged">
        <DataGrid.Columns>
            <DataGridTextColumn Header="ID" Binding="{Binding Path=id}"/>
        </DataGrid.Columns>

    </DataGrid>
</Grid>

And on my code side, ds is a dataset which has one datatable. Each row is a packet 4 columns corresponding to header frame tail and id. I am trying to get the id to bind correctly first. But the id number cannot display on the datagrid.

dgrid.DataContext = ds.Tables[0];

It actually displays the correct data if i do not use the datagridtextcolumn on the xaml side. but what i wanted is to do the binding on the xaml side to the datatable.

2
It seems to be no problem. Can you see any first exception about Binding in Debug window? Or did you check the instance of ds.Tables[0] was initialized correctly? - Jin-Wook Chung
@edelweiss try to remove the binding on the datagrid then just do it on you're code. - Allan Chua
@jwJung When i run it, i saw this in the output window. System.Windows.Data Error: 40 : BindingExpression path error: 'id' property not found on 'object' ''DataRowView' (HashCode=8381127)'. BindingExpression:Path=id; DataItem='DataRowView' (HashCode=8381127); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String'). Which means it actually did not bind right? - kkh
@edelweiss: Yes, it does. The error message means that your DataTable does not have the column named id. I wonder how to relate Packet to DataRow. - Jin-Wook Chung
how is ds.Tables[0] items related to your Packet class? if your binding not work you should show us the ds.Tables[0] columns, because thats the itemssource you bind to - blindmeis

2 Answers

0
votes

Try this

 <Grid >
        <DataGrid AutoGenerateColumns="False" Name="dgrid" SelectionChanged="dgrid_SelectionChanged">
            <DataGrid.Columns>
                <DataGridTextColumn Header="ID" Binding="{Binding Path=id}"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>

    dgrid.ItemsSource = ds.Tables[0].AsDataView();
0
votes

If you have a model class like Packet in your case, then I would suggest to use observable collection instead of DataTable to hold the list of Packets. DataTable's item is of type DataRowView.

I am not even sure how is your DataTable holding a Packet instance?

If your ds.Tables[0].Rows[0] of Packet type or ds.Tables[0].Rows[0][0] is of Packet type?

If so that would cause lot of trouble for binding.