-1

I have a datagridview where I have allowed the user to reorder columns.

However, when the user moves a column to another place I'd like the adjacent column (visibility = false) to move with it.

I've tried to do it in the ColumnDisplayIndexChanged event by setting the displayindex of the adjacent column, however,

I get the following error:

"System.InvalidOperationException: This operation cannot be performed while the DisplayIndex of a column is being adjusted."

bikbak
  • 7
  • 1
  • If the column isn’t visible, why do you want to move it? – stuartd Jan 22 '21 at 23:24
  • This is a terrible hack; somebody please come up with a proper solution, so I can delete it: `using (Timer t = new Timer()) { t.Tick += (ss, ee) => dataGridView1.Columns[col + 1].DisplayIndex = dcol + 1; };` - You need to calculate the right numbers, of course.. – TaW Jan 22 '21 at 23:40
  • I have (different) data in both columns of the same item (amount in 1st, Id in 2nd). So when user moves 1st column 2nd should go with it. 2nd can also made visible under certain circumstances. – bikbak Jan 23 '21 at 15:08
  • Should the timer hack go into ColumnDisplayIndexChanged? – bikbak Jan 23 '21 at 15:12
  • For starters the error should be clear. If you attempt to set _”the displayindex of the adjacent column,”_ IN THE grids `ColumnDisplayIndexChanged` event, then you will get the error you see. This makes sense and it is clear from the error that if you want to change a column’s display index that you CAN NOT change it from the grids `ColumnDisplayIndexChanged` event. So, you are going to have to use a different event or combination of events. This is probably doable however some questions… – JohnG Jan 24 '21 at 06:48
  • Do ALL the columns in the grid have this “Amount-ID” pairing with ID being invisible or only “some” of the columns? There seems to be a lot missing from your approach. Let’s say we move a column “pair” to a different column, when moved, how would you know if the move wasn’t “breaking” an existing “Amount-Id” column pair? In fact, from what I can see, it is almost guaranteed to break one in any move. I am just saying that simply moving the columns in pairs isn’t going to guarantee that this “Amount-ID” paring display order will be maintained. Have you considered this? – JohnG Jan 24 '21 at 06:49
  • 1
    Lastly as already mentioned, “changing” the “DISPLAY INDEX” of an “INVISIBLE” column really makes NO sense. If you want to allow the user to “see” the invisible column, then I would think it would be easier to figure out which “DISPLAY INDEX” to use for which column WHEN the user INDICATES what invisible column they want to “see.” Juggling these invisible indexes appears like a wasted effort. You have to keep in mind, the underlying order NEVER changes. The code is simply juggling what the user sees or in this case even what they don’t see. – JohnG Jan 24 '21 at 06:50

1 Answers1

0

Even though probably no one else is interested, I post my solution. By the way, in order not to "break" the pairs, moving of columns is only allowed, when the Amount columns are displayed only. ID columns are displayed when a checkbox is checked. Currently, it seems to work. Any comments on possible bugs are welcome.

    private void dataGridViewAllBatches_MouseUp(object sender, MouseEventArgs e)
    {
        for (int i = 1; i < dataGridViewAllBatches.Columns.Count; i=i+2)
        {
            if (dataGridViewAllBatches.Columns[i+1].DisplayIndex != dataGridViewAllBatches.Columns[i].DisplayIndex +1)
            {
                dataGridViewAllBatches.Columns[i + 1].DisplayIndex = dataGridViewAllBatches.Columns[i].DisplayIndex + 1;
            }
        }           
    }
bikbak
  • 7
  • 1