0

I have a datagridview with the AllowUserToOrderColumns property set to true.

I also have a column at the end of the grid which has AutoSizeMode set to fill, this serves to fill the grid so that rows look "complete"

How would I mark this column to disable reordering on this column only?

I have tried something like the following

Private Sub dgvAdjustments_ColumnDisplayIndexChanged(sender As Object, e As DataGridViewColumnEventArgs) Handles dgvAdjustments.ColumnDisplayIndexChanged
    If e.Column Is TheFillerColumn Then
        e.Column.DisplayIndex = theGrid.Columns.Count - 1
    End If
End Sub

Which this post How to get dragged column in datagridview when AllowUserToOrderColumns = true sort of suggested

However trying to do what I want to do in this event throws an error as you can't set the display index on a column that is being adjusted.

Regards.

Community
  • 1
  • 1
Matt Skeldon
  • 577
  • 8
  • 23

1 Answers1

1

You can use the SynchronisationContext's Post method.

In C#, you will have for example :

private void dataGridView1_ColumnDisplayIndexChanged(object sender, DataGridViewColumnEventArgs e)
{
    SynchronizationContext.Current.Post(delegate(object o)
    {
        if (Column1.DisplayIndex != 1)
            Column1.DisplayIndex = 1;
    }, null);
}

Also, don't do e.Column.DisplayIndex = ... but TheFillerColumn.DisplayIndex = ... instead, as the moved column can be another one and be moved after TheFillerColumn.

Bioukh
  • 1,888
  • 1
  • 16
  • 27
  • A few modifications and the behaviour is more as I would expect. Its ashame the dragging outline still appears but im sure that once the user has tried a few times and it just reverts back to the original position they will get the idea. (They probably wouldn't even drag it anyway, just precautionary) – Matt Skeldon Apr 01 '15 at 07:27