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

  • Home
  • SEARCH
  • 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 8639125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:55:15+00:00 2026-06-12T10:55:15+00:00

My DGV is populated with values entered by the user (not data-bound). The row

  • 0

My DGV is populated with values entered by the user (not data-bound). The row count is dynamic. Even with the default five rows, though, tabbing from cell to cell and from row to row causes the first row (row 0) to disappear “up and under” the DGV when I get to the last (5th) row / row 4.

I want the 5 (or however many) rows to always stay in the same spot, rather than to move based on where the cursor is.

I don’t know why this is the default behavior in this scenario, and am curious about that, but mostly just need to know how to prevent this bizarreness.

When users add additional rows, I calculate the heights of each row like so:

dataGridViewPlatypi.Rows.Add();
int newRowHeight = dataGridViewPlatypi.Height / dataGridViewPlatypi.RowCount;
for (int i = 0; i < dataGridViewPlatypi.RowCount; i++)
{
    dataGridViewPlatypi.Rows[i].Height = newRowHeight;
}

…this inexact math can lead to some “extra space” at the bottom of the grid when the division has a remainder/modulo, such as: When there are six rows, the height of each row is 26 and leaves 4 pixels over on the bottom of the grid — but again, this unnerving behavior even occurs when the default 5 rows are ensconced in the 160 pixel-high DGV (32 pixels per row).

I don’t know if I need to set some property or write some code for the DGV, a row, a cell, or…???

UPDATE

I’ve found that if I add 2 to the DGV’s height at design time (162 instead of 160, so that each row is 32 and there are 2 pixels “for the DGV” left over, it works with the default 5 rows.

HOWEVER, trying to get it to adjust when the user adds additional rows is proving problematic. The following kludgy code does NOT work:

// Adding one extra pixel is not enough; 2 is the least that can be added to prevent the last phantom grey row from appearing
const int BASE_GRID_HEIGHT_SHIM = 2;
const int DEFAULT_ROW_COUNT = 5;

dataGridViewPlatypi.Rows.Add();

int shimAdjustment = dataGridViewPlatypi.RowCount - DEFAULT_ROW_COUNT;
int newRowHeight = dataGridViewPlatypi.Height / dataGridViewPlatypi.RowCount;
int newGridHeight = (newRowHeight * dataGridViewPlatypi.RowCount) + BASE_GRID_HEIGHT_SHIM + shimAdjustment;

for (int i = 0; i < dataGridViewPlatypi.RowCount; i++)
{
    dataGridViewPlatypi.Rows[i].Height = newRowHeight;
}

dataGridViewPlatypi.Size = new Size(dataGridViewPlatypi.Width, newGridHeight);

NOTE: The pushing up of the rows to hide row 0 and reveal the soft grey underbelly of the DGV does not occur when clicking in column 0; it is only after tabbing from one of the columns to the next column that this “up-pushing” occurs (FWIW).

UPDATE 2

This is how I applied the bountiful answer.

private void buttonAddRow_Click(object sender, EventArgs e)
{
    AddAPlatypusRow();
    for (int i = 0; i < dataGridViewPlatypi.RowCount; i++)
    {
        FreezeBand(dataGridViewPlatypi.Rows[i]);
    }
}
  • 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-06-12T10:55:17+00:00Added an answer on June 12, 2026 at 10:55 am

    Below is a complete sample on using DataGridView’s DataGridViewBand class to freeze any row or column.

    In short, you just need:

    1. Call FreezeBand(YouDataGridView.Rows[0])
    2. dataGridView.ColumnHeadersVisible = false;

    Note: You can even freezed band’s BackColor as you wish. Although in the sample is WhiteSmoke(grey).

        private static void FreezeBand(DataGridViewBand band)
        {
            band.Frozen = true;
            DataGridViewCellStyle style = new DataGridViewCellStyle();
            style.BackColor = Color.WhiteSmoke;
            band.DefaultCellStyle = style;
        }
    

    To see a complete demo look at Microsoft’s DataGridViewBandDemo

    To see a fast demo, create a regular Visual Studio, C# Windows Application project and fill your Program.cs file with the following code.

    using System.Drawing;
    using System.Windows.Forms;
    using System;
    
    public class DataGridViewBandDemo : Form
    {
        #region "form setup"
        public DataGridViewBandDemo()
        {
            InitializeComponent();
            InitializeDataGridView();
        }
    
        DataGridView dataGridView;
        FlowLayoutPanel FlowLayoutPanel1 = new FlowLayoutPanel();
    
        private void InitializeComponent()
        {
            FlowLayoutPanel1.Location = new Point(454, 0);
            FlowLayoutPanel1.AutoSize = true;
            FlowLayoutPanel1.FlowDirection = FlowDirection.TopDown;
            AutoSize = true;
            ClientSize = new System.Drawing.Size(614, 360);
            FlowLayoutPanel1.Name = "flowlayoutpanel";
            Controls.Add(this.FlowLayoutPanel1);
            Text = this.GetType().Name;
        }
        #endregion
    
        #region "setup DataGridView"
    
        private void InitializeDataGridView()
        {
            dataGridView = new System.Windows.Forms.DataGridView();
            Controls.Add(dataGridView);
            dataGridView.Size = new Size(300, 200);
    
            // Create an unbound DataGridView by declaring a
            // column count.
            dataGridView.ColumnCount = 4;
    
            // Set the column header style.
            DataGridViewCellStyle columnHeaderStyle =
                new DataGridViewCellStyle();
            columnHeaderStyle.BackColor = Color.Aqua;
            columnHeaderStyle.Font =
                new Font("Verdana", 10, FontStyle.Bold);
            dataGridView.ColumnHeadersDefaultCellStyle =
                columnHeaderStyle;
    
            // Set the column header names.
            dataGridView.Columns[0].Name = "Recipe";
            dataGridView.Columns[1].Name = "Category";
            dataGridView.Columns[2].Name = "Whatever";
            dataGridView.Columns[3].Name = "Rating";
    
            // Populate the rows.
            string[] row1 = new string[]{"Meatloaf", 
                                            "Main Dish", "boringMeatloaf", "boringMeatloafRanking"};
            string[] row2 = new string[]{"Key Lime Pie", 
                                            "Dessert", "lime juice, evaporated milk", "****"};
            string[] row3 = new string[]{"Orange-Salsa Pork Chops", 
                                            "Main Dish", "pork chops, salsa, orange juice", "****"};
            string[] row4 = new string[]{"Black Bean and Rice Salad", 
                                            "Salad", "black beans, brown rice", "****"};
            string[] row5 = new string[]{"Chocolate Cheesecake", 
                                            "Dessert", "cream cheese", "***"};
            string[] row6 = new string[]{"Black Bean Dip", "Appetizer",
                                            "black beans, sour cream", "***"};
            string[] row7 = new string[]{"Black Bean Dip", "Appetizer",
                                            "black beans, sour cream", "***"};
            string[] row8 = new string[]{"Jelly beans", "Appetizer",
                                            "black beans, sour cream", "***"};
            string[] row9 = new string[]{"Barracuda", "Appetizer",
                                            "black beans, sour cream", "***"};
            object[] rows = new object[] { row1, row2, row3, row4, row5, row6, row7, row8, row9 };
    
            foreach (string[] rowArray in rows)
            {
                dataGridView.Rows.Add(rowArray);
            }
    
                dataGridView.ColumnHeadersVisible = false; // This hides regular column header
            FreezeFirstRow();
            // FreezeFirstColumn(); // Uncomment this line to freeze first column
    
        }
    
        // Freeze the first row.
        private void FreezeFirstRow()
        {
            FreezeBand(dataGridView.Rows[0]);
        }
    
        private void FreezeFirstColumn()
        {
            FreezeBand(dataGridView.Columns[1]);
        }
    
        private static void FreezeBand(DataGridViewBand band)
        {
            band.Frozen = true;
            DataGridViewCellStyle style = new DataGridViewCellStyle();
            style.BackColor = Color.WhiteSmoke;
            band.DefaultCellStyle = style;
        }
    
        #endregion
    
        [STAThreadAttribute()]
        public static void Main()
        {
            Application.Run(new DataGridViewBandDemo());
        }
    }
    

    Well, that is my best shot 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When refreshing my DGV contents (not data-bound, populated in code), I've got this code
How to calculate dgv.Rows.Height int x = dgv1.Rows.Height Rows.Height or dgv1.RowsHeight does not exists.
I'm saving values entered in a DGV to a List <T >; they are
I have the DGV bound to data and all the other controls properly. What
For Each row As DataGridViewRow In DGV.Rows DGV.Rows.RemoveAt(CInt(row.Index.ToString)) Next The above code will remove
I have a problem with data binding my user control into a DGV. When
I've got a DataGridView (DGV) that is not bound to a table in SQL
What is wrong with this code for (int i = 1; i < dgv.Rows.Count;)
DGV has no AllowSort property. How can I prevent user to sort the items?
I have a non databound DGV (no datasource, etc; rows added manually). To filter

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.