On the dynamic scheduling problem of DataGrid

Source: Internet
Author: User
Tags interface sort
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

private bool Bsortdesc = false;

Class in Form1_Load.

SqlCommand cmd = Global.cnn.CreateCommand ();
Cmd.commandtext = "Spgetempinfo";
Cmd.commandtype = CommandType.StoredProcedure;
This.da = new SqlDataAdapter (cmd);
This.ds = new DataSet ();
THIS.DV = new DataView ();
This.da.FillSchema (This.ds, SchemaType.Source);
This.da.Fill (This.ds, "tblempinfo");
this.dv.Table = this.ds.tables["Tblempinfo"];
This.dv.Sort = "Id";

This.dg.DataSource = THIS.DV
DataGridTableStyle ts = new DataGridTableStyle ();
Ts. MappingName = "Tblempinfo";

THIS.DG.TABLESTYLES.ADD (TS);
This.dg.tablestyles[0]. gridcolumnstyles["Comment"]. Width = 0;

Here is the MouseDown event for the DataGrid

private void Dgdoc_mousedown (object sender, System.Windows.Forms.MouseEventArgs e)
{
DataGrid TMPDG = (DataGrid) sender;
Datagrid.hittestinfo hti = Tmpdg.hittest (e.x, e.y);

Switch (hti. Type)
{
Case DataGrid.HitTestType.ColumnHeader:
{
Try
{
int ncolumnindex = Hti. Column;
String strcolumnheader = This.dg.tablestyles[0]. Gridcolumnstyles[ncolumnindex]. HeaderText;

This.bsortdesc =!this.bsortdesc;
if (THIS.BSORTDESC)
{
This.dv.Sort = Strcolumnheader + "ASC";
}
Else
{
This.dv.Sort = Strcolumnheader + "DESC";
}
}
catch (Exception ex)
{
MessageBox.Show (ex. message);
}
}break;
}
}

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)

private void Updatescreen ()
{
This.rtbComment.Clear ();
This.rtbComment.Text = This.dv[bindingcontext[this.dv]. position]["Comment"]. ToString ();


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:




Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.