disallow the user to resize Columns' width in a datagrid

  • Thread starter Thread starter Joe Abou Jaoude
  • Start date Start date
J

Joe Abou Jaoude

HI,
Can I disallow the user to resize Columns' width in a datagrid ?
I fixed the width in the TableStyles.
Regards
 
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
 
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...7f8500d3d43/e72245ecfafa1c65#e72245ecfafa1c65

Best regards,
Sergey Bogdanov
http://www.sergeybogdanov.com
 
Thankx guys, I'll try both solutions..

a question about HitTestInfo :

can i use this property to disallow a user to select a row in a datagrid
?
here's the problem:
In a datagrid that is used only in view mode,I don't want the user to be
able to change the row selections. For this purpose, I disabled the
datagrid, but the disadvantage of this method is that it prevents the
user to scroll the datagrid, and thus can't see all the records.

regards
 
Yes there are 8 HitTestType below is the list,
Best way is to write a class as mentioned by Sergey to disable the
datagrid.

DataGrid.HitTestType.Cell
DataGrid.HitTestType.Caption
DataGrid.HitTestType.ColumnHeader
DataGrid.HitTestType.ColumnResize
DataGrid.HitTestType.ParentRows
DataGrid.HitTestType.RowHeader
DataGrid.HitTestType.RowResize
DataGrid.HitTestType.None

block all the HitTestType each with separate condition
Hope this helps,
Cheers,
Arun.
www.innasite.com
 
That's what I m gonna do...

I didn't know that HitTestType can be that useful.
that's great :)

regards
 
listview

I notice you can't do the same with ListView control ! But i badly need to stop the columnresizing there. Can you pro's please help me ?

Appreciated.

BRGDS Rickard
 
Back
Top