This is a migrated thread and some comments may be shown as answers.

Best place to toggle new row position

1 Answer 92 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Licenses
Top achievements
Rank 1
Licenses asked on 13 Jan 2014, 09:15 AM
Hello,

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)
{
    HandleNewRowPosition();
}

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?

1 Answer, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 15 Jan 2014, 02:20 PM
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.

Regards,
Hristo
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
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 >>
Tags
GridView
Asked by
Licenses
Top achievements
Rank 1
Answers by
Hristo
Telerik team
Share this question
or