Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I prevent my user from sizing columns in my datagrid? 1

Status
Not open for further replies.

bigfoot

Programmer
May 4, 1999
1,779
US
Without overriding the column, is there any other way?
 
The only way that I could think of is to set ColumnHeadersVisible = False.

Also, you might be able to attach to the WidthChanged event for each DataGridColumnStyle and then set the width back to its original value after the user changes it.
 
If I ColumnHeadersVisible = False then I won't have any Column Headers.

I'm going to experiment a bit.

All this should have been built into this class. Someone was lazy at M$.
 
I'm ok on overriding the columns, but not overriding the datagrid. If I override the grid, then I'll have to display it using code and not in the visual designer. True?

5.48 How can I prevent my user from sizing columns in my 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 MyDataGrid : DataGrid
{
protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
{
DataGrid.HitTestInfo hti = this.HitTest(new Point(e.X, e.Y));
if(hti.Type == DataGrid.HitTestType.ColumnResize)
{
return; //no baseclass call
}
base.OnMouseMove(e);
}
}

[VB.NET]
Public Class MyDataGrid
Inherits DataGrid
Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
Dim hti As DataGrid.HitTestInfo = Me.HitTest(New Point(e.X,e.Y))
If hti.Type = DataGrid.HitTestType.ColumnResize Then
Return 'no baseclass call
End If
MyBase.OnMouseMove(e)
End Sub
End Class
 
I'm a bit lost. If I override the grid in code, then how do I display it on a form? I loose the visual side of things, right?
Is there anyway to override it so I can still "draw" the grid on the form?

I'm real new to this. What if I create a custom control using the grid? Will I get all the properties then? Then I can draw it.

Thanks for any and all help.

BF (newbe in .NET)
 
What you mean by

If I override the grid in code, then how do I display it on a form? I loose the visual side of things, right?

As you might have already noticed, Datagrid as such includes only very few basic features and we literally have to write all that we need around it. So what we did was we inherited Datagrid and created our own custom class and we always use this as the base class for all our datagrids.

And what do you mean by loosing visual side of things??? Datagrid always do the column drawings and other formattings at run time, so you will never be able to see them while you design it. Is that what you are looking for?

-Kris
 
I just tried out that sample page you sent me Kris.
I was able not only to subclass the datagrid, but I'm well on my way to understanding creating dll's too.

In the sample project, you don't need to create the form, just the class, and when it's added to the toolbox, then I pick it from another new project to put it on the form, I can draw it without any trouble.
I just needed to see that .net's grid knows how to draw itself.
I thought I would not be able to choose it from the toolbox and "draw" it on the form. That's the part I was missing.

If I could give you 10 starts, I would.

Thanks sooooooooo much for the link.

As soon as I get better at this, I'm going to write a faq on the datagrid since there is not much out there that I could find on it. And I think I found a lot of the querks. LOL

If you don't know the right questions to ask, then you don't get the right (any) answers.
 
Now I want to know what else I can do with this puppy once I have subclassed it. P-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top