Re: disallow the user to resize Columns' width in a datagrid

Tech-Archive recommends: Speed Up your PC by fixing your registry



Well, this solution is a bit complicated than just remove header row though it works either with a small correction:- instead of handle MouseUp event you have to write new class that derived from DataGrid, *override* method OnMouseDown then suppress base implementation for ColumnResize types. For more information see [1]. Here you are C# implementation:

public class DataGridWithFixedColumnWidth : DataGrid
{
	protected override void OnMouseDown(MouseEventArgs e)
	{
		DataGrid.HitTestInfo hitInfo = HitTest(e.X, e.Y);
		// suppress ColumnResize event
 		if (hitInfo.Type == DataGrid.HitTestType.ColumnResize) return;

		base.OnMouseDown (e);
	}
}

[1] http://groups-beta.google.com/group/microsoft.public.dotnet.framework.compactframework/browse_frm/thread/99e5d7f8500d3d43/e72245ecfafa1c65#e72245ecfafa1c65

Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com


Arun wrote:
Write a DataGrid MouseUp event for ColumnResize and RowResize
Try the code below.

private void dataGrid1_MouseUp(object sender, MouseEventArgs e)
			{
				DataGrid myGrid=(DataGrid)sender;
				DataGrid.HitTestInfo myHitInfo = myGrid.HitTest(e.X,e.Y);

				if(myHitInfo.Type == DataGrid.HitTestType.ColumnResize)
				{
					//Do Nothing
				}
			}

Hope this helps,
Cheers,
Arun.
www.innasite.com

.