8

I set for my DataGridView object

AllowUserToOrderColumns = true;

How can I detect columns reordering ?

Tony Abrams
  • 4,505
  • 3
  • 25
  • 32
Milos
  • 1,353
  • 2
  • 18
  • 37

6 Answers6

11

Does handling this event do what you need?

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
BenCr
  • 5,991
  • 5
  • 44
  • 68
  • 8
    Not sure why this is marked as correct as it isn't. This event will be fired in a lot of other circumstances, like re-binding the data table and so on. – Robinson May 10 '13 at 12:46
  • 2
    This fires at least twice for every time the user reorders the columns. Once for the starting point, once for the ending point, and once for every column in between. Depending on what you are doing with the event this could be a lot of overhead. – Wayne Sep 08 '16 at 03:27
6

The "Use ColumnDisplayIndexChanged" event looks like the right one. It worked for me. (I'd add a comment I had the rep for it.)

An event handler for that event will contain e.Column reflecting the new value for that column. The property you're looking for is DisplayIndex. Note that the event will fire for each column that had the DisplayIndex changed.

In vb.net:

Private Sub data_ColumnDisplayIndexChanged(sender As Object, e As System.Windows.Forms.DataGridViewColumnEventArgs) Handles data.ColumnDisplayIndexChanged

    Debug.Print(e.Column.DisplayIndex & vbTab & e.Column.Name)

End Sub

Since the event will fire on startup (multiple times), you might want to add some sort of logic to prevent it from firing when you are adding columns or re-arranging the columns based on prior settings:

Private Sub dataAnts_ColumnDisplayIndexChanged(sender As Object, e As System.Windows.Forms.DataGridViewColumnEventArgs) Handles dataAnts.ColumnDisplayIndexChanged

    If bSortingColumns = False Then
        Debug.Print(e.Column.DisplayIndex & vbTab & e.Column.Name)
    End If

End Sub

Or add an event handler programmatically after your startup code is done.

The MSDN link.

Mmm
  • 682
  • 9
  • 11
3

I suggest you...

1 - Do a static int variable.

2 - Affect this variable in the handler :: ColumnHeaderMouseClick

3 - Choose your line with this variable in the handler :: Sorted

Example:

  private static int idRequetePourtriage = -1;


  private void dgvRequete_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
  {
      if (dgvRequete.SelectedRows.Count > 0)
          idRequetePourtriage = Convert.ToInt32(dgvRequete.SelectedRows[0].Cells[TEXT_colNameIdRequete].Value.ToString());

  }

  private void dgvRequete_Sorted(object sender, EventArgs e)
  {
      desactivateGridSelected();


      int rowCount = 0;
      Boolean isFind = false;

      while (rowCount < dgvRequete.Rows.Count && !isFind)
      {
          if (idRequetePourtriage == Convert.ToInt32(dgvRequete.Rows[rowCount].Cells[TEXT_colNameIdRequete].Value.ToString()))
          {
              isFind = true;
              activateGridSelected();

              dgvRequete.Rows[rowCount].Selected = true;
          }
          rowCount++;
      }

      if (!isFind)
      {
          activateGridSelected();
      }
  }
Alexander Vogt
  • 17,879
  • 13
  • 52
  • 68
1

Use ColumnDisplayIndexChanged event

Kevin Ng
  • 11
  • 1
0

I don't know what exactly you are trying to achieve. If you want to add a custom column sort behavior, you could have a look on this tutorial on customizable column sorting.

Basically, you catch the MouseDown event there and you look whether the user clicked on a column header. If he did and there is an event assigned to it, this can be executed.

Sören
  • 2,661
  • 2
  • 19
  • 22
0

The best option I found was to put code in the CellMouseUp event. When a user drags a column and drops up, the CellMouseUp event will be called after the columns have been rearranged.

ColumnDisplayIndexChanged gets called too often, including when the form is initializing which can cause issues depending on what you need the event to do.

Garrett Banuk
  • 111
  • 1
  • 3