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 60893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:13:59+00:00 2026-05-10T18:13:59+00:00

I am inserting a column in a DataGridView programmatically (i.e., not bound to any

  • 0

I am inserting a column in a DataGridView programmatically (i.e., not bound to any data tables/databases) as follows:

int lastIndex = m_DGV.Columns.Count - 1;  // Count = 4 in this case DataGridViewTextBoxColumn col = (DataGridViewTextBoxColumn)m_DGV.Columns[lastIndex]; m_DGV.Columns.RemoveAt(lastIndex); m_DGV.Columns.Insert(insertIndex, col);  // insertIndex = 2 

I have found that my columns are visually out of order sometimes using this method. A workaround is to manually set the DisplayIndex property of the column afterwards. Adding this code ‘fixes it’, but I don’t understand why it behaves this way.

Console.Write(m_DGV.Columns[0].DisplayIndex); // Has value of 0 Console.Write(m_DGV.Columns[1].DisplayIndex); // Has value of 1 Console.Write(m_DGV.Columns[2].DisplayIndex); // Has value of 3 Console.Write(m_DGV.Columns[3].DisplayIndex); // Has value of 2 col.DisplayIndex = insertIndex; Console.Write(m_DGV.Columns[0].DisplayIndex); // Has value of 0 Console.Write(m_DGV.Columns[1].DisplayIndex); // Has value of 1 Console.Write(m_DGV.Columns[2].DisplayIndex); // Has value of 2 Console.Write(m_DGV.Columns[3].DisplayIndex); // Has value of 3 

As an aside, my grid can grow its column count dynamically. I wanted to grow it in chunks, so each insert didn’t require a column allocation (and associated initialization). Each ‘new’ column would then be added by grabbing an unused column from the end, inserting it into the desired position, and making it visible.

  • 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. 2026-05-10T18:14:00+00:00Added an answer on May 10, 2026 at 6:14 pm

    I suspect this is because the order of the columns in the DataGridView do not necessarily dictate the display order, though without explicitly being assigned by default the order of the columns dictate the DisplayIndex property values. That is why there is a DisplayIndex property, so you may add columns to the collection without performing Inserts – you just need to specify the DisplayIndex value and a cascade update occurs for everything with an equal or greater DisplayIndex. It appears from your example the inserted column is also receiving the first skipped DisplayIndex value.

    From a question/answer I found:

    Changing the DisplayIndex will cause all the columns between the old DisplayIndex and the new DisplayIndex to be shifted.

    As with nearly all collections (other than LinkedLists) its always better to add to a collection than insert into a collection. The behavior you are seeing is a reflection of that rule.

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

Sidebar

Ask A Question

Stats

  • Questions 73k
  • Answers 73k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer There is no built in support for reading most of… May 11, 2026 at 2:07 pm
  • added an answer There are actually quite a few alternative implementations. The reason… May 11, 2026 at 2:07 pm
  • added an answer You're kind of in luck. There is an explosion of… May 11, 2026 at 2:07 pm

Related Questions

If I am inserting elements into a wrap panel and there is still space
Say I am inheriting from a class with several overloaded constructors. By any chance
I am using the django framework and am using templates, inheriting a lot of
I'm working on a winform app and am interating through a checkboxlist to see
Previously I've asked about inserting a column into a dataset . I now have
There have been similar questions but the answers weren't what I was looking for.
I'm using Linq to SQL. I have a DataContext against which I am .SubmitChanges()'ing.
I know that I can insert multiple rows using a single statement, if I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.