In recent development, using the DataGrid Class (System.Windows.Forms), it is convenient to click on the column headings to sort the table data by that column, but sometimes the user wants to sort through the initial data.
Let's start with two little things you need to know:
1, Datagrid.allowsorting Property
Gets or sets a value that indicates whether the grid can be reordered by clicking the column header.
2, Datagrid.hittesttype Enumeration
Specifies the part that the user clicks in the System.Windows.Forms.DataGrid control.
Where: None represents the background area that is visible when the control does not contain a table, or when there are fewer rows, or when the table scrolls to its bottom.
And when the user clicks on the row header and the small rectangular area to the right of the column header, the resulting Hittesttype value is None
So we can set the AllowSorting property of the DataGrid object to restore the initial sort of tabular data when the user clicks the None area.
The program is very simple, add the following to the MouseDown event of the DataGrid object
DataGrid dg = (DataGrid) sender;
Datagrid.hittestinfo hit = DG. HitTest (e.x, e.y);
if (hit. Type.equals (DataGrid.HitTestType.None))
{
Dg. AllowSorting = false;
Dg. AllowSorting = true;
}
What does this have to do with event handling for the. NET framework?
If I wanted to create a class that was derived from the DataGrid and implemented just the functionality, I would need to overload a method of the DataGrid class, so we went to the topic.
In C #, an overloaded event method name begins with "on" and is declared "virtual," and a method declared as virtual means that it can be overloaded.
Overloaded Event method,
Event method raises an event.
Raising an event invokes an event handler
For more information about raising an event, see Raising an event
I'm in the OnMouseDown event method because it raises the MouseDown event. With the idea, the program is simple, as follows:
Note the last sentence base. OnMouseDown (e);
When overriding OnMouseDown in a derived class, be sure to call the OnMouseDown method of the base class.
The next time you use the DataGrid, you can do it through the Exdatagrid class, as follows:
Using Myextend;
Exdatagrid dg = new Exdatagrid ();
Well, writing here should be achieved my purpose, more knowledge can access the MSDN Library, really very detailed
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service