block user from resizing datagrid rows

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello everyone,

Is there a way to prevent the user from changing the column width of a
datagrid?
I set them with a tablestyle and I don't want the user to mess up with it.

Also does anyone knows a way to format a datetime within a datagrid with the
compact framework?

Thanks,
Sitar.
 
Hi,

You have to create a class that inherits from datagrid for that. Here
is an example. Hope this helps.


Dim ds As New DataSet
Dim dg As New NoResizeDataGrid

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim strPath As String
strPath = System.IO.Path.GetDirectoryName( _

System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
ds.ReadXml(strPath & "\Categories.xml")

With dg
.Location = New Point(10, 10)
.Visible = True
End With

Me.Controls.Add(dg)
dg.DataSource = ds.Tables(0)
End Sub
End Class

Public Class NoResizeDataGrid
Inherits DataGrid


Protected Overrides Sub OnMouseDown(ByVal e As
System.Windows.Forms.MouseEventArgs)
Dim hti As DataGrid.HitTestInfo = Me.HitTest(e.X, e.Y)

If hti.Type = DataGrid.HitTestType.ColumnResize Or hti.Type =
DataGrid.HitTestType.RowResize Then

Return 'no baseclass call

End If

MyBase.OnMouseDown(e)
End Sub

Public Sub New()

End Sub

Protected Overrides Sub OnMouseMove(ByVal e As
System.Windows.Forms.MouseEventArgs)
Dim hti As DataGrid.HitTestInfo = Me.HitTest(e.X, e.Y)

If hti.Type = DataGrid.HitTestType.ColumnResize Or hti.Type =
DataGrid.HitTestType.RowResize Then

Return 'no baseclass call

End If

MyBase.OnMouseMove(e)
End Sub


End Class

Ken
-------------------
 
Hi,

Thanks for the sample code. I tried to use it on the mousedown/mousemove
event of my datagrid instead, but it does not work. Why so? It should still
trap the even, am I wrong?

Thanks,
Sitar.
 
Hi,

I check and see if the mouse is over the row or column resize area in
the onmousedown an onmousemove procedures. If it is I return from the
procedure so the datagrid does not go into the resize mode. If the
mouse is anywhere else I let the datagrid handle it.

Ken
----------------
 
Hi Ken,

I understand what you code does. It's just that it seems it does not work. I
translated your code into c# and put it inside a windows form for the mouse
down/move events of my datagrid and it does nothing. The events are fired
though but the code does not give the desired result.

Any idea why?
Thanks.
Sitar.


private void dgListe_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
DataGrid.HitTestInfo hitTest = dgListe.HitTest(e.X, e.Y);

if (hitTest.Type == DataGrid.HitTestType.ColumnResize || hitTest.Type ==
DataGrid.HitTestType.ColumnHeader)
{
return;
}
}

private void dgListe_MouseMove(object sender,
System.Windows.Forms.MouseEventArgs e)
{
DataGrid.HitTestInfo hitTest = dgListe.HitTest(e.X, e.Y);

if (hitTest.Type == DataGrid.HitTestType.ColumnResize || hitTest.Type ==
DataGrid.HitTestType.ColumnHeader)
{
return;
}
}
 
Back
Top