Search code examples
c#scrolldatagridviewpanelheader-row

How to have a DataGridView inside a panel that has a header-row that scrolls with horizontal but not with vertical when the panel scrolls


I have a DataGridView that uses a Panel for scroll to avoid the dreaded "Jump to beginning of list" DataGridView refresh issue.

What I'm looking for, is a header-row that stays visible during vertical scroll and moves along with horizontal scroll.

I tested this code in Visual Studio 2012, so it should run out of the box.

Form1.designer.cs

namespace DataGridViewPanel
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.panel1 = new System.Windows.Forms.Panel();
            this.dataGridView1 = new System.Windows.Forms.DataGridView();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.panel1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.SuspendLayout();
            // 
            // panel1
            // 
            this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.panel1.AutoScroll = true;
            this.panel1.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.panel1.Controls.Add(this.dataGridView1);
            this.panel1.Location = new System.Drawing.Point(12, 73);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(260, 177);
            this.panel1.TabIndex = 0;
            // 
            // dataGridView1
            // 
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Location = new System.Drawing.Point(0, 0);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.ScrollBars = System.Windows.Forms.ScrollBars.None;
            this.dataGridView1.Size = new System.Drawing.Size(257, 174);
            this.dataGridView1.TabIndex = 0;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(13, 41);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 1;
            this.button1.Text = "Clear";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(197, 41);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 2;
            this.button2.Text = "Add Row";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.panel1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.panel1.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.DataGridView dataGridView1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
    }
}

Form.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DataGridViewPanel
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            fillDataGrid();
        }

        private void fillDataGrid()
        {
            dataBase Db = new dataBase();
            List<dataBase> DbList = new List<dataBase>();
            for (int i = 0; i < 10; i++)
            {
                DbList.Add(Db);
            }

            dataGridView1.DataSource = DbList;
            int width = 0;
            int height = 0;
            foreach (DataGridViewColumn col in dataGridView1.Columns) width += col.Width;
            foreach (DataGridViewRow row in dataGridView1.Rows) height += row.Height;
            dataGridView1.Size = new Size(width, height);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            dataBase Db = new dataBase();
            List<dataBase> DbList = new List<dataBase>();

            DbList.Add(Db);


            dataGridView1.DataSource = DbList;
            int width = 0;
            int height = 0;
            foreach (DataGridViewColumn col in dataGridView1.Columns) width += col.Width;
            foreach (DataGridViewRow row in dataGridView1.Rows) height += row.Height;
            dataGridView1.Size = new Size(width, height);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            dataBase Db = new dataBase();
            List<dataBase> DbList2 = new List<dataBase>();

            DbList2 = (List<dataBase>)dataGridView1.DataSource;

            dataGridView1.DataSource = null;

            DbList2.Add(Db);


            dataGridView1.DataSource = DbList2;
            int width = 0;
            int height = 0;
            foreach (DataGridViewColumn col in dataGridView1.Columns) width += col.Width;
            foreach (DataGridViewRow row in dataGridView1.Rows) height += row.Height;
            dataGridView1.Size = new Size(width, height);
        }


    }

    public class dataBase : IEnumerable<string>
    {
        public string one { get; set; }
        public string two { get; set; }
        public string three { get; set; }
        public string four { get; set; }
        public string five { get; set; }
        public string six { get; set; }
        public string seven { get; set; }
        public string eight { get; set; }
        public string nine { get; set; }
        public string ten { get; set; }

        public IEnumerator<string> GetEnumerator()
        {
            yield return one;
            yield return two;
            yield return three;
            yield return four;
            yield return five;
            yield return six;
            yield return seven;
            yield return eight;
            yield return nine;
            yield return ten;
        }
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }
    }
}

Solution

  • I achieved a workable solution with an overlapping panel. I created a second panel and DataGridView. I populate the second datagrid with the header. The panel1.Scroll is handled on horizontal scroll to move the panel2 scroll. Because panel1 overlaps the scrollbar of panel2, a final panel1.BringToFront() in the scroll handler makes sure panel1 hides panel2 scrollbars.

    Form1.Designer.cs

    namespace DataGridViewPanel
    {
        partial class Form1
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;
    
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }
    
            #region Windows Form Designer generated code
    
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
            this.panel1 = new System.Windows.Forms.Panel();
            this.dataGridView1 = new System.Windows.Forms.DataGridView();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.panel2 = new System.Windows.Forms.Panel();
            this.dataGridView2 = new System.Windows.Forms.DataGridView();
            this.panel1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.panel2.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView2)).BeginInit();
            this.SuspendLayout();
            // 
            // panel1
            // 
            this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.panel1.AutoScroll = true;
            this.panel1.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
            this.panel1.Controls.Add(this.dataGridView1);
            this.panel1.Location = new System.Drawing.Point(13, 107);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(260, 207);
            this.panel1.TabIndex = 0;
            this.panel1.Scroll += panel1_Scroll;
            // 
            // dataGridView1
            // 
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.ColumnHeadersVisible = false;
            this.dataGridView1.Location = new System.Drawing.Point(0, 0);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.ReadOnly = true;
            this.dataGridView1.ScrollBars = System.Windows.Forms.ScrollBars.None;
            this.dataGridView1.Size = new System.Drawing.Size(257, 204);
            this.dataGridView1.TabIndex = 0;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(13, 41);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 1;
            this.button1.Text = "Clear";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(197, 41);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 2;
            this.button2.Text = "Add Row";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // panel2
            // 
            this.panel2.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.panel2.AutoScroll = true;
            this.panel2.Controls.Add(this.dataGridView2);
            this.panel2.Location = new System.Drawing.Point(13, 84);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(260, 42);
            this.panel2.TabIndex = 1;
            // 
            // dataGridView2
            // 
            this.dataGridView2.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView2.Location = new System.Drawing.Point(0, 0);
            this.dataGridView2.Name = "dataGridView2";
            this.dataGridView2.Size = new System.Drawing.Size(257, 40);
            this.dataGridView2.TabIndex = 3;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 326);
            this.Controls.Add(this.panel2);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.panel1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.panel1.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
            this.panel2.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView2)).EndInit();
            this.ResumeLayout(false);
    
            }
    
    
    
            #endregion
    
            private System.Windows.Forms.Panel panel1;
            private System.Windows.Forms.DataGridView dataGridView1;
            private System.Windows.Forms.Button button1;
            private System.Windows.Forms.Button button2;
            private System.Windows.Forms.Panel panel2;
            private System.Windows.Forms.DataGridView dataGridView2;
        }
    }
    

    Form1.cs

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace DataGridViewPanel
    {
        public partial class Form1 : Form
        {
            private dataBase Db = new dataBase();
            private List<dataBase> DbList = new List<dataBase>();
            private List<dataBase> headList = new List<dataBase>();
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                fillDataGrid();
            }
    
            private void fillDataGrid()
            {
                //add ten rows to the List
                for (int i = 0; i < 10; i++)
                {
                    DbList.Add(Db);
                }
    
                //add one row to the List to populate headers
                headList.Add(Db);
    
                //populate datagrid by setting datasource
                dataGridView1.DataSource = DbList;
                dataGridView2.DataSource = headList;
    
                resizeDataGrids();
    
            }
    
            private void resizeDataGrids()
            {
                //resize the datagrids so the panels enable the scrollbars when required
    
                int width = 0;
                int height = 0;
                foreach (DataGridViewColumn col in dataGridView1.Columns) width += col.Width;
                foreach (DataGridViewRow row in dataGridView1.Rows) height += row.Height;
                dataGridView1.Size = new Size(width, height);
    
                width = 0;
                height = 0;
                foreach (DataGridViewColumn col in dataGridView2.Columns) width += col.Width;
                foreach (DataGridViewRow row in dataGridView2.Rows) height += row.Height;
                dataGridView2.Size = new Size(width, height);
    
                //bring panel1 to front
                panel1.BringToFront();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                //populate datagridview1 with an empty List
                DbList = new List<dataBase>();
                DbList.Add(Db);
                dataGridView1.DataSource = DbList;
                resizeDataGrids();
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                //populate datagridview1 with List after adding a new row to the List
                DbList = (List<dataBase>)dataGridView1.DataSource;
                dataGridView1.DataSource = null;
                DbList.Add(Db);
                dataGridView1.DataSource = DbList;
                resizeDataGrids();
            }
    
            void panel1_Scroll(object sender, System.Windows.Forms.ScrollEventArgs e)
            {
                //Handle horizontal scroll
                if (e.ScrollOrientation == System.Windows.Forms.ScrollOrientation.HorizontalScroll)
                {
    
                    panel2.HorizontalScroll.Value = panel1.HorizontalScroll.Value;
                    panel1.BringToFront();
                }
            }
    
        }
    
        public class dataBase : IEnumerable<string>
        {
            public string one { get; set; }
            public string two { get; set; }
            public string three { get; set; }
            public string four { get; set; }
            public string five { get; set; }
            public string six { get; set; }
            public string seven { get; set; }
            public string eight { get; set; }
            public string nine { get; set; }
            public string ten { get; set; }
    
            public IEnumerator<string> GetEnumerator()
            {
                yield return one;
                yield return two;
                yield return three;
                yield return four;
                yield return five;
                yield return six;
                yield return seven;
                yield return eight;
                yield return nine;
                yield return ten;
            }
            System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
            {
                return this.GetEnumerator();
            }
        }
    }