Home Forums WPF controls Xceed DataGrid for WPF changing property value in datasource question

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #22914 |

    Hi,

    I have a question about the way updates in the data source are handled.

    This is the situation:
    My DataGrid is bound to an object datasource. Two of the properties of the object are product code and color code. When the grid is in edit mode I would like to display a color picker dialog after the user has entered a product code.
    I have added an event handler to the LostFocus event of the product code TextBox to display the color picker…so far so good.
    But when I change the value of the color code property in the event handler and then close the dialog again, the new value is not displayed in the datagrid.

    I have tried the Refresh method on the Items property of the grid, but then the focus is moved to the first editable column in the row in stead of to the next column.

    This is the code I used for the event handler:

    private void ProductCodeLostFocus(object sender, RoutedEventArgs e)
    {
    DataRow row = OrdersGrid.ItemContainerGenerator.ContainerFromItem(OrdersGrid.CurrentItem) as DataRow;
    if (null!= row && row.Cells[“Product.Code”].IsDirty)
    {
    OrderLine CurrentOrderLine = OrdersGrid.CurrentItem as OrderLine;

    ColorPickerWindow ColorPicker = new ColorPickerWindow(row.Cells[“Product.Code”].Content.ToString());
    if (ColorPicker.ShowDialog().Value)
    {
    CurrentOrderLine.Product.Color = ColorPicker.ListBoxColors.SelectedValue.ToString();
    //OrdersGrid.Items.Refresh();
    }
    ColorPicker = null;
    }
    }

    Is there a way to get this to work properly without the Refresh method?
    Your help is much appreciated!

    Cheers
    Arjan

    Imported from legacy forums. Posted by Arjan (had 6351 views)

    User (Old forums)
    Member
    Post count: 23064

    Does your object datasource implement INotifyPropertyChanged? (Namespace: System.ComponentModel)
    I think that is the reason why your Grid is not updated.

    Try this:

    public class ObjectDataSource : INotifyPropertyChanged
    {
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String info)
    {
    if (PropertyChanged != null)
    {
    PropertyChanged(this, new PropertyChangedEventArgs(info));
    }
    }

    private int property1 = 0;
    public int Property1
    {
    get
    {
    return this.property1;
    }
    set
    {
    this.property1 = value;
    NotifyPropertyChanged(“property1”);
    }
    }
    }

    Hth
    Silas

    Imported from legacy forums. Posted by Ralf (had 294 views)

    User (Old forums)
    Member
    Post count: 23064

    Thanks Silas!
    As you suggested I implemented INotifyPropertyChanged and now the datagrid shows my updated property value….BUT only when I call row.EndEdit() manually.
    This causes a new problem: due to calling EndEdit the next column that receives focus is not in edit mode so I have to click on it again to start editing….I can already here the complaints from our users 🙂
    I have tried calling BeginEdit but that does not do the trick.

    Any ideas?

    Thanks!

    Arjan

    BTW this is now the code I use:

    private void ProductCodeLostFocus(object sender, RoutedEventArgs e)
    {
    DataRow row = OrdersGrid.ItemContainerGenerator.ContainerFromItem(OrdersGrid.CurrentItem) as DataRow;
    if (null!= row && row.Cells[“Product.Code”].IsDirty)
    {
    OrderLine CurrentOrderLine = OrdersGrid.CurrentItem as OrderLine;

    ColorPickerWindow ColorPicker = new ColorPickerWindow(row.Cells[“Product.Code”].Content.ToString());
    ColorPicker.Owner = App.Current.MainWindow;
    ColorPicker.ShowInTaskbar = false;
    ColorPicker.WindowStartupLocation = WindowStartupLocation.CenterOwner;

    if (ColorPicker.ShowDialog().Value)
    {
    row.EndEdit();
    CurrentOrderLine.Product.Color = ColorPicker.ListBoxColors.SelectedValue.ToString();
    row.BeginEdit();
    }
    ColorPicker = null;
    }
    }

    Imported from legacy forums. Posted by Arjan (had 580 views)

    User (Old forums)
    Member
    Post count: 23064

    How are you doing the data binding?
    Are you using ObjectDataProvider in XAML or are you doing it in C#?

    I am working on a similar application and it works fine. The only problem i got left is that one needs to hit return or has to change the line in the Grid to achieve an update of the dataset. I would prefer, that the Dataset would be updated when a Cell is left… Dunno yet how to do that.

    Try databinding like this:

    ObjectDataProvider:

    <Window.Resources>
    <ObjectDataProvider x:Key=”Model” ObjectType=”{x:Type namespace:Model}”/>
    </Window.Resources>

    DataGrid:

    <xcdg:DataGridControl x:Name=”OrdersGrid” AutoCreateColumns=”True”
    ItemsSource=”{Binding Path=Model, Mode=Default, Source={StaticResource Model}}”/>

    Hth
    Silas

    Imported from legacy forums. Posted by Ralf (had 449 views)

    User (Old forums)
    Member
    Post count: 23064

    Currently I’m binding the data in C#….I will give the other option a try but really there shouldn’t be a difference.

    Regarding your problem: currently I don’t think it is possible (@anybody from XCeed: correct me if I’m wrong! :-)). You can try creating your own custom DataCell class and implement an CellEndedEditEvent. I have seen something similar in this forum but then for InsertionCellEndedEditEvent. If you search the forum for this you will find a code example.

    Thanks again!

    Arjan

    Imported from legacy forums. Posted by Arjan (had 406 views)

    User (Old forums)
    Member
    Post count: 23064

    Changing the binding to ObjectDataProvider did not help…
    I think the problem is that while the row is in Edit mode, programmatic changes to column values are not available until EndEdit is called.

    Hopefully someone from XCeed can shed some light on this..

    Cheers
    Arjan

    Imported from legacy forums. Posted by Arjan (had 952 views)

    User (Old forums)
    Member
    Post count: 23064

    Why do you do the changes to the grid and not to the object instead?

    I changed from edit mode to “normal” mode when the user hits the tab. I tryed to avoid such event handling because it makes the whole thing more fault-prone. Maby that helps you too.

    Somewhere in the constructor:

    MyGrid.KeyUp += new KeyEventHandler(MyGrid_KeyUp);

    Event:

    private void MyGrid_KeyUp(object sender, KeyEventArgs args)
    {
    if (args.Key == Key.Tab)
    {
    Xceed.Wpf.DataGrid.DataRow dr = OrdersGrid.ItemContainerGenerator.ContainerFromItem(MyGrid.CurrentItem) as Xceed.Wpf.DataGrid.DataRow;
    if (dr != null)
    dr.EndEdit();
    }
    }

    Imported from legacy forums. Posted by Ralf (had 319 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi,
    My previous posting was not clear: I do make the changes to the object itself, not to the grid.

    Changing the way the grid responds to Tab event is not an option for me…but thanks for the suggestion anyway.

    Cheers
    Arjan

    Imported from legacy forums. Posted by Arjan (had 6082 views)

    User (Old forums)
    Member
    Post count: 23064

    I also have a similar issue in I have a grid bound to a DataTable. I have to call EndEdit when I make any changes to the DataRow before the changes are reflected in the data grid e.g. code such as 

     

                if (e.Column.ColumnName == “Transaction Type” &&
                    e.ProposedValue.ToString().Contains(“DDI”))
                {
                    e.Row[“Amount”] = 0.0M;
                    e.Row.EndEdit();
                }

     

    Is there any property I can set in the grid to ensure the changes are reflected straightaway with any change to the bound data source. Surely I don’t have to create an event handler in this way on the grid to achieve this.  I know there are various property values that can be set through the UpdateSourceTrigger property e.g. CellContentChanged which control how the data source is updated in relation to updates in the grid by the user.

    I’d like to simply update the grid as soon as any updates occur to the data source row and now have to call EndEdit everywhere.

     

     

    Imported from legacy forums. Posted by Matt (had 2554 views)

Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.