Best place to toggle new row position

2 posts, 0 answers
  1. Sodi We
    Sodi We avatar
    160 posts
    Member since:
    Apr 2010

    Posted 13 Jan 2014 Link to this post


    We have implemented custom insert logic in our grids via the context menu.
    However, when there are no items in the grid, the user can't use the context menu so we show the "Click here to insert new row".

    Once there's an item in the collection, the "Click here to insert new row" is no longer shown.

    We are having problems pinpointing the best location at which to handle this.

    We check if the grid doesn't have any items and if the "CanUserInsertRows" property is set to true.
    If these conditions both are true, we show the insert new row area, otherwise we hide it:

    public void HandleNewRowPosition()
        var showNewRow = !GridTreeListHasItems() && Grid.CanUserInsertRows;
        Grid.NewRowPosition = GetInsertNewRowPosition(showNewRow);
    private bool GridTreeListHasItems()
        var anyValue = false;
        var x = Grid.ItemsSource as IEnumerable;
        if (x != null)
            anyValue = x.GetEnumerator().MoveNext();
        return anyValue;
    private GridViewNewRowPosition GetInsertNewRowPosition(bool show)
        var converter = new BooleanToGridViewNewRowPositionConverter();
        return (GridViewNewRowPosition)converter.Convert(show, typeof(GridViewNewRowPosition), null, CultureInfo.CurrentUICulture);
     /// <summary>
    /// Converts a boolean to the position of new rows. True corresponds with the top.
    /// </summary>
    [ValueConversion(typeof(bool), typeof(GridViewNewRowPosition))]
    public class BooleanToGridViewNewRowPositionConverter : IValueConverter
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            return (bool) value ? GridViewNewRowPosition.Top : GridViewNewRowPosition.None;
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            return (GridViewNewRowPosition) value == GridViewNewRowPosition.Top;

    I tried adding a value changed callback to the "CanUserInsertRows" dependency property, but it doesn't seem to trigger the callback when I do a Notify property changed on the property which is bound to the "CanUserInsertRows" property of the grid:

    _canUserInserRowsPropertyDescriptor = DependencyPropertyDescriptor.FromProperty(GridViewDataControl.CanUserInsertRowsProperty, typeof(GridViewDataControl));
    if (_canUserInserRowsPropertyDescriptor != null)
        _canUserInserRowsPropertyDescriptor.AddValueChanged(this.AssociatedObject, CanUserInserRowsChanged_Handler);
    private void CanUserInserRowsChanged_Handler(object sender, EventArgs e)

    We used to call the "HandleNewRowPosition" method at several places (eg "OnItemsChanged", "OnLoaded", ...) but we noticed that it got called way to many times when we only expected it once, maybe twice.

    Anyone have an idea on how to do this properly?

  2. Hristo
    Hristo avatar
    41 posts

    Posted 15 Jan 2014 Link to this post

    Hi Sodi,

    You have to call the method HandleNewRowPosition from the  GridView's DataLoaded event.
    Also there is no need for the method GridTreeListHasItems, because the GridView has a boolean property HasItems, which is set to  false if there are no items. Please note that the HasItems property doesn't have setter.

    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  3. DevCraft R3 2016 release webinar banner
Back to Top