DataGridView handle column reordering event
I set for my DataGr开发者_Go百科idView object
AllowUserToOrderColumns = true;
How can I detect columns reordering ?
Does handling this event do what you need?
ColumnDisplayIndexChanged
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.
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();
}
}
Use ColumnDisplayIndexChanged event
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.
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.
精彩评论