datagrid| Dynamic | sort | Problem I have this design interface design habit: I like to filter out the "Comment" field of a database from any control that displays a list of databases, and then by tracking the clicks of the user's mouse, Dynamically displaying its contents next to another piece I think it's more appropriate to display it, such as a editbox (Multiline = True). It used to be a good move because I didn't use a DataGrid, but rather a control like a ListBox, but the powerful features of the DataGrid gave me a problem. Because in the DataGrid interface, users can change the order of data display by clicking ColumnHeader. In my Code, in the beginning, only by overriding the MouseDown event, you can arrange to read the data from the local DataView and display it elsewhere by using the row number or index of the current row of the DataGrid. Later I found that when the user clicked on the ColumnHeader data in the DataGrid after the order changed, in the next display of information, such as comment, is not on the number.
I later found that each time I clicked on the Gatagrid ColumnHeader, the small arrows on it changed direction, but the arrow direction of each columnheader was independent. Say that they are independent because the arrows in each column change direction only after they are clicked, and if you click on another column, this will have no effect on this column. For example, there are two columns, from the direction of the arrow, the data is sorted by ID column in ascending order:
Id∧name
Now that the user clicks on the first columnheader, the result becomes the following:
Id∨name
At this point, if the user continues to click on the first column, the direction of the arrow will change to the top, and then click the latter, the arrow direction will be set, not up, because it is the default in ascending order.
If there is a way to unify these arrows, I can dynamically change the sort property of the DataView to match the new Order, Because by capturing the MouseDown event in the DataGrid I can get the headertext of the clicked ColumnHeader.
So how do you make these arrows all unified?
With a Boolean variable!
Yes, that's simple, but it's not easy to think about. So I set a Boolean variable in the current form class, named Bsortdesc and initialized it to false, because this is the default. Then, when the user clicks on the "column header", I get the column name in the MouseDown event in the DataGrid. The Boolean variable is evaluated as a reverse transformation to determine what the DataView Sort property will be changed to. In this way, the background of the data view will dynamically follow the foreground variables and change the order. As a then, the other data displayed next to it can be on the number.
Here's a piece of code to explain:
Declaring a protected Boolean variable in the definition of the Form1 class
If you choose to display some fields outside the DataGrid and bind them to the data source, join the following CurrentCellChanged event handling code
private void Dgdoc_currentcellchanged (object sender, System.EventArgs e)
{
Now the DG Row index can is treated as the row index of the data in
DV Since DV has been re-sorted
Bindingcontext[this.]. Position = This.dg.CurrentRowIndex;
This. Updatescreen ();
}
Finally, the method of updating the interface (RTB representative RichTextBox)
This way, when you click on the ColumnHeader of the DataGrid, the arrow on the top is always in the same direction, even if you click on a different column. As for the backstage view, it has been rearranged dynamically.
This is my own way out, there may be deficiencies, if anyone has a better way, I hope to share with you D:
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.