Matt Skeldon
Matt Skeldon

Reputation: 595

Datagridview Cancel Drag and Drop on Filler Column

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.

Upvotes: 0

Views: 325

Answers (1)

Bioukh
Bioukh

Reputation: 1968

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.

Upvotes: 1

Related Questions