Datagridview error : Value' should be between 'minimum' and 'maximum' - dataGridView1.PerformLayout() does no work

Aleš N 1 Reputation point
2021-06-02T11:03:35.327+00:00

Hello

I am having troubles with datagridview when I want to have data partialy loaded and load next/previous data by scrolling (putting scrollbar with mouse to limit positions also).

This topic is much discussed, but mostly suggested answer "adding dataGridView1.PerformLayout()" does not works for me (https://social.microsoft.com/Forums/en-US/0b091621-34a5-45e4-a37c-c7e18a767a26/datagridview-scroll-error-minimum-and-maximum-values?forum=Offtopic https://social.msdn.microsoft.com/Forums/en-US/2dc39584-d833-4989-bb70-9b616a9daf4b/datagridview-throws-exception-when-navigating-with-arrow-keys?forum=csharpgeneral).

Due to this, I have created simple test form only with datagridview, code:

    using System;
using System.Linq;
using System.Windows.Forms;
namespace DataGridViewTester
{
    public delegate void DeleteTableDelegate(DataGridView table);
    public delegate void FillRowsDelegate(DataGridView table, int rowsToAdd);
    public partial class Form1 : Form
    {
        VScrollBar vScrollbar;
        int index = 0;
        public Form1()
        {
            InitializeComponent();
            vScrollbar = dataGridView1.Controls.OfType<VScrollBar>().First();

            vScrollbar.ValueChanged += new EventHandler(this.scrollbarValue_Changed);
            AppendRange(dataGridView1, 100);

        }

        public void ClearTableContents(DataGridView table)
        {
             //For calling from different threads
             if (table.InvokeRequired)
             {
                 var d = new DeleteTableDelegate(ClearTableContents);
                 table.Invoke(d, new object[] { table });
                 return;
             }

            try
            {
                table?.Rows.Clear();
            }
            catch (InvalidOperationException ex) {
                Console.WriteLine(ex);
            }

            table?.PerformLayout();
        }

        public void AppendRange(DataGridView table, int rowsToAdd)
        {
            if (table.InvokeRequired)
            {
                var d = new FillRowsDelegate(AppendRange);
                table.Invoke(d, new object[] { table, rowsToAdd });
                return;
            }
            Console.WriteLine(rowsToAdd);
            DataGridViewRow[] dataGridViewRows = new DataGridViewRow[(rowsToAdd < 0 ? -rowsToAdd : rowsToAdd)];
            index = (rowsToAdd >= 0) ? (index ) : (index - 2*rowsToAdd);
            for (int i = 0; i < dataGridViewRows.Length; i++)
            {
                dataGridViewRows[i] = new DataGridViewRow();
                dataGridViewRows[i].CreateCells(dataGridView1, new object[] { index + i, dataGridViewRows[i].GetHashCode() });
            }
            index = (rowsToAdd >= 0) ? (index+ rowsToAdd) : (index);

            table?.Rows.AddRange(dataGridViewRows);
            table?.PerformLayout();
        }

        private void scrollbarValue_Changed(Object sender, EventArgs e)
        {
            if (vScrollbar.Visible)
            {
                if (vScrollbar.Value + (vScrollbar.LargeChange - 1) >= vScrollbar.Maximum)
                {
                    System.Console.WriteLine("Get NEXT results " + vScrollbar.Maximum + ">=" + vScrollbar.Value + ">=" + vScrollbar.Minimum + " Large change -1 = " + (vScrollbar.LargeChange - 1));

                    try
                    {
                        ClearTableContents(dataGridView1);
                        Console.WriteLine("CLEARED " + vScrollbar.Maximum + ">=" + vScrollbar.Value + ">=" + vScrollbar.Minimum + " Large change -1 = " + (vScrollbar.LargeChange - 1));
                        AppendRange(dataGridView1, 1); //Append it to Grid
                        Console.WriteLine("APENDED " + vScrollbar.Maximum + ">=" + vScrollbar.Value + ">=" + vScrollbar.Minimum + " Large change -1 = " + (vScrollbar.LargeChange - 1));

                    }
                    catch (System.ArgumentOutOfRangeException ex)
                    {
                        Console.WriteLine(ex);
                    }

                    System.Console.WriteLine(vScrollbar.Maximum + ">=" + vScrollbar.Value + ">=" + vScrollbar.Minimum + " Large change -1 = " + (vScrollbar.LargeChange - 1));
                }
                else if (vScrollbar.Value <= vScrollbar.Minimum)
                {
                    System.Console.WriteLine("Get PREVIOUS results " + vScrollbar.Maximum + ">=" + vScrollbar.Value + ">=" + vScrollbar.Minimum + " Large change -1 = " + (vScrollbar.LargeChange - 1));


                    try
                    {
                        ClearTableContents(dataGridView1);
                        Console.WriteLine("CLEARED " + vScrollbar.Maximum + ">=" + vScrollbar.Value + ">=" + vScrollbar.Minimum + " Large change -1 = " + (vScrollbar.LargeChange - 1));
                        AppendRange(dataGridView1, -1); //Append it to Grid
                        Console.WriteLine("APENDED " + vScrollbar.Maximum + ">=" + vScrollbar.Value + ">=" + vScrollbar.Minimum + " Large change -1 = " + (vScrollbar.LargeChange - 1));
                        dataGridView1?.PerformLayout();
                    }
                    catch (System.ArgumentOutOfRangeException ex)
                    {
                        Console.WriteLine(ex);
                    }


                    System.Console.WriteLine(vScrollbar.Maximum + ">=" + vScrollbar.Value + ">=" + vScrollbar.Minimum + " Large change -1 = " + (vScrollbar.LargeChange - 1));
                }
            }
        }

    }
}

Thank you for your answers

EDIT: This behaviour is caused holding scrollbar by mouse. If this paging is mapped to PageDown and PageUp buttons, there are no errors.

Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
{count} votes

1 answer

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,661 Reputation points
    2021-06-04T07:12:26.42+00:00

    Hi AleN-3800,
    The problem lies in vScrollbar.Value.
    And I change your following codes:

    if (vScrollbar.Value + (vScrollbar.LargeChange - 1) >= vScrollbar.Maximum)  
    else if (vScrollbar.Value <= vScrollbar.Minimum)  
    

    to

     if (vScrollbar.Value  >= vScrollbar.Maximum)  
     else if (vScrollbar.Value <vScrollbar.Minimum)  
    

    Then it will not appear error.
    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.