WPF RadGridView add new row in NOT edit mode.

1 Answer 59 Views
GridView
Shukurdin
Top achievements
Rank 1
Shukurdin asked on 20 May 2024, 04:01 PM
Hi! I'm trying to add new row in NOT edit mode, but new row always in edit mode. Is it possible to add new row in NOT edit mode?

Example: 


public class CustomKeyboardCommandProvider : DefaultKeyboardCommandProvider
	{
		private GridViewDataControl parentGrid;

		public CustomKeyboardCommandProvider(GridViewDataControl grid)
		 : base(grid)
		{
			this.parentGrid = grid;
		}

		public override IEnumerable<ICommand> ProvideCommandsForKey(Key key)
		{
			List<ICommand> commandsToExecute = base.ProvideCommandsForKey(key).ToList();

			if (key == Key.Enter)
			{
				commandsToExecute.Clear();
				commandsToExecute.Add(RadGridViewCommands.CommitEdit);
				commandsToExecute.Add(RadGridViewCommands.BeginInsert);
				commandsToExecute.Add(RadGridViewCommands.CommitEdit);
				
				this.parentGrid.ChildrenOfType<GridViewScrollViewer>().First().ScrollToEnd();
			}

			return commandsToExecute;
		}
	}

1 Answer, 1 is accepted

Sort by
0
Stenly
Telerik team
answered on 23 May 2024, 08:40 AM

Hello,

To achieve this requirement, you could create a new attached property for the GridViewNewRow element, which will subscribe to its PreviewMouseLeftButtonDown event. In the event handler, the e.Handled property can be set to true (so that the default logic will not be executed) and you could add a new entry of the object to your collection.

The following code snippets show this suggestion's implementation:

Sample model and view model:

public class Club
{
    public Club()
    {
        
    }

    public string Name { get; set; }
    public int StadiumCapacity { get; set; }
}

public class MainViewModel
{
    public MainViewModel()
    {
        this.Clubs = new ObservableCollection<Club>()
        {
            new Club() { Name = "Arsenal", StadiumCapacity = 50000 },
            new Club() { Name = "Manchester United", StadiumCapacity = 60000 },
            new Club() { Name = "Chelsea", StadiumCapacity = 40000 },
        };
    }

    public ObservableCollection<Club> Clubs { get; set; }
}

 

<Grid>
    <Grid.DataContext>
        <local:MainViewModel/>
    </Grid.DataContext>
    <Grid.Resources>
        <Style TargetType="telerik:GridViewNewRow">
            <Setter Property="local:GridViewNewRowExtensions.ShouldAddNewItemInNonEditMode" Value="True"/>
        </Style>
    </Grid.Resources>
    <telerik:RadGridView ItemsSource="{Binding Clubs}"
                         GroupRenderMode="Flat"
                         NewRowPosition="Top"/>
</Grid>

The implementation of the GridViewNewRowExtensions class is as follows:

public class GridViewNewRowExtensions
{
    public static bool GetShouldAddNewItemInNonEditMode(DependencyObject obj)
    {
        return (bool)obj.GetValue(ShouldAddNewItemInNonEditModeProperty);
    }

    public static void SetShouldAddNewItemInNonEditMode(DependencyObject obj, bool value)
    {
        obj.SetValue(ShouldAddNewItemInNonEditModeProperty, value);
    }

    public static readonly DependencyProperty ShouldAddNewItemInNonEditModeProperty =
        DependencyProperty.RegisterAttached("ShouldAddNewItemInNonEditMode", typeof(bool), typeof(GridViewNewRowExtensions), new PropertyMetadata(false, OnShouldAddNewItemInNonEditModeChanged));

    private static void OnShouldAddNewItemInNonEditModeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if ((bool)e.NewValue)
        {
            GridViewNewRow gridViewNewRow = (GridViewNewRow)d;

            gridViewNewRow.PreviewMouseLeftButtonDown += GridViewNewRow_PreviewMouseLeftButtonDown;
        }
    }

    private static void GridViewNewRow_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        GridViewNewRow gridViewNewRow = (GridViewNewRow)sender;

        MainViewModel mainViewModel = gridViewNewRow.GridViewDataControl.DataContext as MainViewModel;

        if (mainViewModel != null)
        {
            mainViewModel.Clubs.Insert(0, new Club());
        }

        e.Handled = true;
    }
}

The logic in the PreviewMouseLeftButtonDown event prevents the default logic and adds a new item to the collection bound to the RadGridView's ItemsSource property.

I have attached a sample project for you to test.

Regards,
Stenly
Progress Telerik

A brand new ThemeBuilder course was just added to the Virtual Classroom. The training course was designed to help you get started with ThemeBuilder for styling Telerik and Kendo UI components for your applications. You can check it out at https://learn.telerik.com
Tags
GridView
Asked by
Shukurdin
Top achievements
Rank 1
Answers by
Stenly
Telerik team
Share this question
or