0

I have a datagrid like this:

<dg:DataGrid Name="dg" AutoGenerateColumns="False" CanUserDeleteRows="True">
                    <dg:DataGrid.Columns>
                        <dg:DataGridTextColumn Header="Product Code" x:Name="columnProductCode" Binding="{Binding Path=Product.ProductCode}" IsReadOnly="True" ></dg:DataGridTextColumn>
                        <dg:DataGridTextColumn Header="Product Name" x:Name="columnProductName" Binding="{Binding Path=Product.Name}" IsReadOnly="True" ></dg:DataGridTextColumn>
                        <dg:DataGridTextColumn Header="ProductMeasure" x:Name="columnDonViTinh" Binding="{Binding Path=Product.Measure IsReadOnly="True"></dg:DataGridTextColumn>
                        <dg:DataGridTextColumn Header="Quantity" x:Name="ColumnQuantity" Binding="{Binding Path=Quantity IsReadOnly="False"></dg:DataGridTextColumn>
                    </dg:DataGrid.Columns>
</dg:DataGrid>

In the behind code, I have a struct like this:

private struct ProductDetail
        {

            public TProduct Product { get; set ; } // TProduct is a class provied by a web service
            public int Quantity { get; set; }
        }

and a List like this:

        private IList<ProductDetail> bs = new List<ProductDetail>();

I had tried to fill data to "bs". And binding like this:

this.dg.ItemsSource = this.bs;

Everything is ok. I can insert a new row, delete row, but when I try to modified the column Quantity then click on the header of the datagrid (to resort) --> the Quantity column change to it is before.

How can I fix this problem. Thanks advanced.

3 Answers 3

4

On your datagrid, you must specify that the data changes when you change the property, rather than leave the cell. To do that, you add the UpdateSourceTrigger=PropertyChanged to your binding and then it should work. Your DataGrid will now look like this:

<dg:DataGrid Name="dg" AutoGenerateColumns="False" CanUserDeleteRows="True">
    <dg:DataGrid.Columns>
        <dg:DataGridTextColumn Header="Product Code"
            x:Name="columnProductCode" 
            Binding="{Binding Path=Product.ProductCode, UpdateSourceTrigger=PropertyChanged}"
            IsReadOnly="True" >
        </dg:DataGridTextColumn>
        <dg:DataGridTextColumn Header="Product Name"
            x:Name="columnProductName"
            Binding="{Binding Path=Product.Name, UpdateSourceTrigger=PropertyChanged}"
            IsReadOnly="True" >
        </dg:DataGridTextColumn>
        <dg:DataGridTextColumn Header="ProductMeasure"
            x:Name="columnDonViTinh"
            Binding="{Binding Path=Product.Measure, UpdateSourceTrigger=PropertyChanged}"
            IsReadOnly="True">
        </dg:DataGridTextColumn>
        <dg:DataGridTextColumn Header="Quantity"
            x:Name="ColumnQuantity"
            Binding="{Binding Path=Quantity, UpdateSourceTrigger=PropertyChanged}" 
            IsReadOnly="False">
        </dg:DataGridTextColumn>
    </dg:DataGrid.Columns>
</dg:DataGrid>
1
  • Thanks Mike Brown and Brent. I have try "UpdateSourceTrigger=PropertyChanged" but it does not work. Apr 25, 2010 at 15:25
1

I don't know the full API of the DataGrid but I think you have to commit changes before they are pushed back to the underlying object. Looking at the docs It looks like the edit isn't committed until you change cells or press enter. If you sort before doing either of those actions, the edit might be cancelled.

1
  • Thanks Mike Brown and Brent. I have try "UpdateSourceTrigger=PropertyChanged" but it does not work. Apr 25, 2010 at 15:26
1

OK, I have try to define a class (with TProduct and Quantity attribute also) instead of the current struct. And .. it works =.=

private class ProductDetail
{
    public TProduct Product { get; set ; } // TProduct is a class provied by a web service
    public int Quantity { get; set; }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.