Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How do I prevent the user from resizing columns in a DataGrid?

You can do this by subclassing your grid and overriding OnMouseMove, and not calling the baseclass if the point is on the columnsizing border.

[C#]

public class CustomDataGrid : DataGrid
{
  // prevent display of resizing cursor
  protected override void OnMouseMove( MouseEventArgs e )
  {
    DataGrid.HitTestInfo hti = HitTest( new Point( e.X, e.Y ) );
    if ( hti.Type == DataGrid.HitTestType.ColumnResize )
      return; // don't call base class

    base.OnMouseMove( e );
  }

  // prevent user from resizing column
  protected override void OnMouseDown( MouseEventArgs e )
  {
    DataGrid.HitTestInfo hti = HitTest( new Point( e.X, e.Y ) );
    if ( hti.Type == DataGrid.HitTestType.ColumnResize )
      return; // don't call base class

    base.OnMouseDown( e );
  }
}

George Shepherd, Syncfusion, and Stephen Muecke



Page view counter