How does winform load database data to the tree and the winform tree?

Source: Internet
Author: User

How does winform load database data to the tree and the winform tree?
I. Introduction

For how to load xml to generate the directory tree in winform, in the previous article "c # how to load the tree directory from xml, and the complete Text is displayed. I have already written the detailed process. However, in some cases, it is impossible to store a large amount of data in xml Because xml is generally used to transmit small data volumes. However, we generally use specialized database management tools to store and transmit big data. Here, we declare that for the transmission of large data volumes, we do not consider the speed transmission speed here. For example, we only implement the most basic functions, this makes it easier for beginners to understand and understand.
Well, after talking about so much nonsense, we started to officially enter the subject of our article:
"How does winform load database data to the tree "?
To better display the expected results, I will first show you the implementation diagram of a system that I have previously done, as shown below:

Now, it is displayed! If this is what you want, I will show you how to make this tree structure in the form of code.

Ii. Detailed implementation process

To achieve the above results, we need to do the following two steps:

    dataRowView = gridX.Rows[rowIndex].DataBoundItem as DataRowView;    dataRow = dataRowView.Row;

In this way, the obtained data is stored in the DataTable object, and the row data is stored in dataRow.
By reloading the constructor, we pass these two parameters. The constructor is as follows (the other two parameters are determined based on different conditions and can be left empty ):

Public Organizations (DataTable transTable, DataRow transRow, String parent, String cloumnName) {InitializeComponent (); orgTable = new DataTable (); orgTable. tableName = "record"; // DataTable table name, which is the same as that on the client and server. Contact orgTable = transTable; getRow = transRow; ParentOrgID = Global. userParentOrg; // Global is the static class I declare. You can obtain various static variables. UserOrgID = Global. userOrgID; name = cloumnName; functionsParent = parent ;}

Now, we need to Load the data to the TreeView control. For specific code, we can unload the Load event. The specific code is as follows:

Private void Organizations_Load (object sender, EventArgs e) {# region defines the Variable List <TreeNode> treeNodes; // DataRow dataRow; DataRow [] dataRows; TreeNode treeNode, tempNode; NameValueCollection parameter; string str; TreeNode [] nodes; String orgID, parentOrgID; # endregion # region create tree treeNodes = new List <TreeNode> (); # region add query root node dataRows = orgTable. select ("[orgID] = \ '" + userOrgID + "\'"); treeNode = New TreeNode (); tag = "orgID = {0} & orgName = {1} & parent = {2} & fullName = {3}"; tag = String. format (tag, dataRows [0] ["orgID"], dataRows [0] ["orgName"], dataRows [0] ["parent"], dataRows [0] ["fullName"]); treeNode. name = Convert. toString (dataRows [0] ["orgID"]); treeNode. text = Convert. toString (dataRows [0] ["orgName"]); treeNode. tag = tag; treeNodes. add (treeNode); // Add the root node to treeViewX in the treeNodes list. nodes. add (treeNode ); // Load the root node in the TreeView. # Endregion # Add a subnode while (treeNodes. count> 0) {treeNode = treeNodes [0]; // each cycle removes the first node. The next cycle is the second node treeNodes. remove (treeNode); tag = treeNode. tag as String; parameter = System. web. httpUtility. parseQueryString (tag); str = "[parent] = '{0}'"; str = String. format (str, parameter ["orgID"]); dataRows = orgTable. select (str); for (int I = 0; I <dataRows. length; I ++) {tag = "orgID = {0} & orgName = {1} & p Arent = {2} & fullName = {3} "; tag = String. format (tag, dataRows [I] ["orgID"], dataRows [I] ["orgName"], dataRows [I] ["parent"], dataRows [I] ["fullName"]); tempNode = new TreeNode (); tempNode. tag = tag; tempNode. name = Convert. toString (dataRows [I] ["orgID"]); tempNode. text = Convert. toString (dataRows [I] ["orgName"]); treeNodes. add (tempNode); // Add the child node treeNode of the parent node found above. nodes. add (tempNode); // set all subnodes in the treeNode to tempN. Ode is added to its "" }}# endregion # region selects the recorded org code // This code is judged based on the condition, the objective is to make the default selection based on the conditions. If (functionsParent = "F005100") {if (name = "orgName") {parentOrgID = Convert. toString (getRow ["parentOrg"]); nodes = treeViewX. nodes. find (parentOrgID, true); // query the specific node if (nodes. length> 0) {nodes [0]. checked = true ;}} else if (name = "branchName") {orgID = Convert. toString (getRow ["orgID"]); nodes = treeViewX. nodes. find (orgID, true); if (nodes. length> 0) {nodes [0]. checked = true ;}} else {orgID = Convert. toString (getRow ["orgID"]); nodes = treeViewX. nodes. find (orgID, true); if (nodes. length> 0) {nodes [0]. checked = true ;}# endregion}

In fact, the loading tree has been generated. Next I will write more, click "OK", and then how to put the data in the control.

Private void okButton_Click (object sender, EventArgs e) {# region defines the variable TreeNode selectedNode; List <TreeNode> nodes; TreeNode treeNode; NameValueCollection paramters; # endregion # region selectedNode = null; nodes = new List <TreeNode> (); // note TreeView. nodes is the root node of the obtained control. For (int I = 0; I <treeViewX. nodes. count; I ++) {nodes. add (treeViewX. nodes [I]);} // cyclically traverse from the root node and check whether each node is selected. While (nodes. count> 0) {treeNode = nodes [0]; nodes. remove (treeNode); if (treeNode. checked = true) {selectedNode = treeNode; break;} for (int I = 0; I <treeNode. nodes. count; I ++) {nodes. add (treeNode. nodes [I]) ;}# endregion # region read information and rewrite the data if (selectedNode! = Null) {tag = selectedNode. tag as String; paramters = System. web. httpUtility. parseQueryString (tag); getRow ["orgID"] = paramters ["orgID"]; getRow ["orgName"] = paramters ["orgName"];} else {getRow ["orgID"] = ""; getRow ["orgName"] = "";} this. close (); # endregion}

So far, our tree is basically built! The reason is that the basic build is complete because the following problems occur when we click the directory tree:

That is, we can select multiple items, but when we click OK, we get the first option. The expected result is a single choice, and the selection items can be modified cyclically in this form. How to implement it?
By searching through msdn, we will find an event called "BeforeCheck" (which occurs before the checkbox of the selected tree node ), it delegates the TreeViewCancelEventHandler (BeforeCheck, BeforeCollapse, BeforeExpand, or BeforeSelect delegates the event method ). The note contains the following sentence: "Unless the delegate is removed, the event handler is called whenever this event occurs ."
That is to say, we actually call the default BeforCheck format. We can call the BeforeCheck event multiple times, that is, we can click to select multiple times. (In fact, we can obtain the answer based on the remarks .)
We can't help but have the following problems:
Question 1: How to solve multiple choices?
We can re-write the BeforeCheck event and let it be removed first, that is, it cannot be selected, and then call the modified BeforeCheck at the end.
Question 2: How to modify and select in this dialog box
Recursive call
Now that we understand the solution, we can re-Modify the BeforeCheck event. The specific code is as follows:

Private void treeViewX_BeforeCheck (object sender, TreeViewCancelEventArgs e) {List <TreeNode> nodes; Int32 intX; TreeNode treeNode; // you have to manually modify check to disable treeViewX. beforeCheck-= new TreeViewCancelEventHandler (treeViewX_BeforeCheck); nodes = new List <TreeNode> (); for (intX = 0; intX <treeViewX. nodes. count; intX = intX + 1) {nodes. add (treeViewX. nodes [intX]);} // traverses the while (nodes. count> 0) {treeNode = nodes [0]; nodes. remove (treeNode); treeNode. checked = false; for (intX = 0; intX <treeNode. nodes. count; intX = intX + 1) {nodes. add (treeNode. nodes [intX]) ;}// e. node. checked = true; treeViewX. beforeCheck + = new TreeViewCancelEventHandler (treeViewX_BeforeCheck); // recursive call}

Add at the end of the Load event:

 treeViewX.BeforeCheck += new TreeViewCancelEventHandler(treeViewX_BeforeCheck);

So far, our build tree has been completed!

---- Good luck!

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.