Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3962068
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:03:06+00:00 2026-05-20T03:03:06+00:00

I am currently working with data grid where I only want to allow the

  • 0

I am currently working with data grid where I only want to allow the user to enter UP TO 20 rows of data before making CanUserAddRows to false.

I made a dependency property on my own datagrid (that derives from the original one). I tried using the event “ItemContainerGenerator.ItemsChanged” and check for row count in there and if row count = max rows, then make can user add row = false (I get an exception for that saying i’m not allow to change it during “Add Row”.

I was wondering if there is a good way on implementing this ….

Thanks and Regards,
Kevin

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T03:03:06+00:00Added an answer on May 20, 2026 at 3:03 am

    Here’s a subclassed DataGrid with a MaxRows Dependency Property. The things to note about the implementation is if the DataGrid is in edit mode and CanUserAddRows is changed an InvalidOperationException will occur (like you mentioned in the question).

    InvalidOperationException
    ‘NewItemPlaceholderPosition’ is not
    allowed during a transaction begun by
    ‘AddNew’.

    To workaround this, a method called IsInEditMode is called in OnItemsChanged and if it returns true we subscribe to the event LayoutUpdated to check IsInEditMode again.

    Also, if MaxRows is set to 20, it must allow 20 rows when CanUserAddRows is True, and 19 rows when False (to make place for the NewItemPlaceHolder which isn’t present on False).

    It can be used like this

    <local:MaxRowsDataGrid MaxRows="20"
                           CanUserAddRows="True"
                           ...>
    

    MaxRowsDataGrid

    public class MaxRowsDataGrid : DataGrid
    {
        public static readonly DependencyProperty MaxRowsProperty =
            DependencyProperty.Register("MaxRows",
                                        typeof(int),
                                        typeof(MaxRowsDataGrid),
                                        new UIPropertyMetadata(0, MaxRowsPropertyChanged));
        private static void MaxRowsPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
        {
            MaxRowsDataGrid maxRowsDataGrid = source as MaxRowsDataGrid;
            maxRowsDataGrid.SetCanUserAddRowsState();
        }
        public int MaxRows
        {
            get { return (int)GetValue(MaxRowsProperty); }
            set { SetValue(MaxRowsProperty, value); }
        }
    
        private bool m_changingState;
        public MaxRowsDataGrid()
        {
            m_changingState = false;
        }
        protected override void OnItemsChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            base.OnItemsChanged(e);
            if (IsInEditMode() == true)
            {
                EventHandler eventHandler = null;
                eventHandler += new EventHandler(delegate
                {
                    if (IsInEditMode() == false)
                    {
                        SetCanUserAddRowsState();
                        LayoutUpdated -= eventHandler;
                    }
                });
                LayoutUpdated += eventHandler;
            }
            else
            {
                SetCanUserAddRowsState();
            }
        }
        private bool IsInEditMode()
        {
            IEditableCollectionView itemsView = Items;
            if (itemsView.IsAddingNew == false && itemsView.IsEditingItem == false)
            {
                return false;
            }
            return true;
        }        
        // This method will raise OnItemsChanged again 
        // because a NewItemPlaceHolder will be added or removed
        // so to avoid infinite recursion a bool flag is added
        private void SetCanUserAddRowsState()
        {
            if (m_changingState == false)
            {
                m_changingState = true;
                int maxRows = (CanUserAddRows == true) ? MaxRows : MaxRows-1;
                if (Items.Count > maxRows)
                {
                    CanUserAddRows = false;
                }
                else
                {
                    CanUserAddRows = true;
                }
                m_changingState = false;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The cms I'm currently working with only supports live editing of data (news, events,
I an a graduate student of nuclear physics currently working on a data analysis
I'm currently working on a reasonably complicated data input form, based around ASP.NET Web
I'm currently working in a piece of code where both logic and data access
I'm currently working on a Silverlight app and need to convert XML data into
I am currently working on a project where I a data matching algorithm needs
I am currently working on a Silverlight 4.0 Application that displays about 13.000 rows
I am currently working on a data input form that will take as in
Currently working in the deployment of an OFBiz based ERP, we've come to the
Currently working on a VBScript to automate some of the dirty PST ingestion work

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.