Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am reordering the Columns in my DataGridView1. I a moving for example, Columns[3](which is the 4th position) into second position, in place of Columns[1].
So from
0123456789 - this is the normal order--------and it SHOULD become:
0312456789 - this is how it should be--------But after reorder:
0231456789 - how is finally reordering

I was happily saving and loading all this indexes-List and some Columns keep jumping around when I was loading the winapp. A couple of them were fine, they kept their original location after I move them, but one was just moving to the >>right by 1 index every time I reposition it (I reposition first and save and load and it jumped after was loaded). I hope is clear.
I chased and debug the code and I find out it is ONLY the datagrid reordering problem. Not my saving and loading. I made a TESTCODE and it clearly show that is indeed messing up the indexes. NOT all of them, but only specific ones. I am thinking 2 things:
1- my dataGridView1.Columns[i].DisplayIndex is not the right way of reading these indexes?
2- there are some settings I screw up when I was initially playing with datagridview. But... I looked as close as patiently as I could and every Columns (Collection) in DesignView is the same with each other. I check it 3 times and I cant see anything diferent from 1 to another. I know for sure I play around with 1 or 2 of them but definetly not all of them, so the rest are the default , and I compare everything (by looking) with the default ones. Maybe 1 super mischivious property is still not correct there?
I simply have no idea what it could be. I only know, it is messing up (very randomly) this datagridview Index. Very strange!
Maybe you have a clue what it can be?
So far this Datagridview component is doing a formidable job, I love it so far. Except some bumps on the road but it is a very good component.
Thank you again.

What I have tried:

C#
List<int> ListCO = new List<int>();

        private void dataGridView1_ColumnDisplayIndexChanged(object sender, DataGridViewColumnEventArgs e)
        {
            ListCO.Clear();
            for (int i = 0; i < dataGridView1.Columns.Count; i++)
            {
                ListCO.Add(dataGridView1.Columns[i].DisplayIndex);
            }
            int debug = 0;
        }


UPDATE !!!
I made a fresh new project and I remade this entire setup:
and the results are the same. So its not from my code.
https://i.imgur.com/pnq1Hc7.jpg[^]
and
https://i.imgur.com/dt5WiJM.jpg[^]
The steps are as following:
Add a new DataGridView component to your Form1
In DataGridView properties goto: Columns (Collections) and add 10 new Columns.
Select DataGridView component and from it's little top right arrow expandable menu, check "Enable Column Reordering".
Assign the event to it and paste my code and run it.
In run time, move column 4 into #2 place (so not the first but the second)
Now read my List order.
Posted
Updated 18-Nov-21 2:29am
v8

1 solution

i can't replicate the behavior you describe, are you sure you are taking into account that Columns are 1-based index /

there is a glitch where the moved Column may have its 'AutotoSize property reset to the default. you can handle that by:
dataGridView1.Columns[3].DisplayIndex = 1; // column named "4" moved to Column 1's position

dataGridView1.Columns[3].AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.ColumnHeader;
dataGridView1.Invalidate();
 
Share this answer
 
Comments
_Q12_ 3-Nov-21 10:32am    
In my original post, I put an Update with some steps that I made.
Hope it will be helpful for you to replicate my problem.
BillWoodruff 3-Nov-21 11:24am    
i'm unable to replicate the behavior in the update you describe.

before

name Column1 displayindex : 0
name Column2 displayindex : 1
name Column3 displayindex : 2
name Column4 displayindex : 3
name Column5 displayindex : 4

dataGridView1.Columns[4].DisplayIndex = 2;

after

name Column1 displayindex : 0
name Column2 displayindex : 1
name Column3 displayindex : 3
name Column4 displayindex : 4
name Column5 displayindex : 2

i do know that weird results can occur if you move column #0 to another location, or move a > #0 column to #0.

put breakpoints before and after you set the DisplayIndex, and examine the runtime Columns state.
_Q12_ 18-Nov-21 8:44am    
I am explaining this problem as simple as possible:
Here is exactly what I do: (and try to do it Exactly as Im doing it,in your side)
step1: I made a new project and added a new datagrid and in it added 10 columns as showing in the picture; also I didnt change any properties of the columns Ive added, I left them by default.
https://i.imgur.com/ENfuA2W.jpg[^]

step2: I put a breakpoint in my code, then I run the project and drag column4 in between column1 and column2 as shown in the image. What is interesting that here in runtime, it is showing correctly the order after I drag the columns around. But in code, the index of the columns are randomized. So to be clear, this runtime order is the correct order, and it is 142356789. 4 is indeed in between 1 and 2!!!
https://i.imgur.com/CqJ8x9E.jpg[^]

step3: ListCO is my --DEBUG LIST--. After putting my breakpoint, I can read the entire List content and its order. That is shown in the next image. But the order is not as in runtime, it is 023[1]456789, so 1 jumped in between 3 and 4. WHYYYYYYYY? (It should have been 1[4]2356789) What do I miss? What is the logic behind this column reorder?
https://i.imgur.com/THB9SD8.jpg[^]
As always, thank you !

My entire TesT code here:

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

List<int> ListCO = new List<int>();
private void dataGridView1_ColumnDisplayIndexChanged(object sender, DataGridViewColumnEventArgs e)
{
ListCO.Clear();
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
ListCO.Add(dataGridView1.Columns[i]..DisplayIndex);
}
int debug = 0;
}
}



I have tried with:
ListCO.Add(dataGridView1.Columns[i].Index);
but indiferent of the runtime column reorder, the list will always show 0123456789,with absolutly no changes.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900