Milos
Milos

Reputation: 1465

DataGridView handle column reordering event

I set for my DataGridView object

AllowUserToOrderColumns = true;

How can I detect columns reordering ?

Upvotes: 9

Views: 18385

Answers (6)

Garrett Banuk
Garrett Banuk

Reputation: 131

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.

Upvotes: 0

BenCr
BenCr

Reputation: 6052

Does handling this event do what you need?

Upvotes: 12

Mmm
Mmm

Reputation: 961

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.

Upvotes: 6

Kevin Ng
Kevin Ng

Reputation: 11

Use ColumnDisplayIndexChanged event

Upvotes: 1

Jimbo James
Jimbo James

Reputation: 31

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();
      }
  }

Upvotes: 3

S&#246;ren
S&#246;ren

Reputation: 2741

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.

Upvotes: 0

Related Questions