0

I have a TreeView and on a separate panel 3 checkboxes. The Treeview data is being pulled from a database,this database has two tables Product and Model inside the Model table there are 3 columns - twoDoor, threeDoor and fiveDoor. My TreeView looks like this;

CAR(PRODUCT) ->FORD(MODEL) ->BMW(MODEL) ->VW(MODEL) 

On a separate panel there are 3 checkboxes;

  • Two Door
  • Three Door
  • Five Door

The state of these checkboxes are already defined in the Model db. How can i display their state against whatever child Node(Model) is picked in the TreeView?

My Code:

 private void Form1_Options_Load(object sender, EventArgs e) { String connectionString; connectionString = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString; conn = new SqlConnection(connectionString); String Sequel = "SELECT id,ProductName,ModelType FROM Product"; SqlDataAdapter da = new SqlDataAdapter(Sequel, conn); DataTable dt = new DataTable(); conn.Open(); da.Fill(dt); foreach (DataRow dr in dt.Rows) { parentNode = treeView1.Nodes.Add(dr["ProductName"].ToString()); //treeView1.Nodes.Add(dr["ModelType"].ToString()); PopulateTreeView(Convert.ToInt32(dr["Id"].ToString()), parentNode); } } public void PopulateTreeView(int parentId, TreeNode parentNode) { String Seqchildc = "Select * From Model WHERE ProductId = "+ parentId + ""; SqlDataAdapter dachildmnuc = new SqlDataAdapter(Seqchildc, conn); DataTable dtchildc = new DataTable(); dachildmnuc.Fill(dtchildc); TreeNode childNode; foreach (DataRow dr in dtchildc.Rows) { if (parentNode == null) childNode = treeView1.Nodes.Add(dr["ModelName"].ToString()); //childNode.Nodes.Add(dr["ModelType"].ToString()); else childNode = parentNode.Nodes.Add(dr["ModelName"].ToString()); //PopulateTreeView(Convert.ToInt32(dr["Id"].ToString()), childNode); } treeView1.ExpandAll(); //Connect(); } 
5
  • How do you populate your tree view, i.e. is there some association between TreeNode and Product/Model classes? Commented Apr 11, 2016 at 15:52
  • Hi Thanks for the reply, Yes i have a Product.cs and Model.cs Commented Apr 11, 2016 at 15:59
  • Hi. I mean, when creating nodes, do you put the objects you use somewhere - for instance in the node Tag? Commented Apr 11, 2016 at 16:01
  • I have added the Form Load , a PopulateTreeView. Commented Apr 11, 2016 at 16:04
  • Could i do something like this? - ` string two = dr["twoDoor"].ToString(); ckbTwoDoor.Items.add(two,true);` Commented Apr 11, 2016 at 16:30

1 Answer 1

1

Here is what you can do if I understand correctly the question.

First, create a class to hold your Model information:

class ModelInfo { public string Name { get; set; } public bool TwoDoor { get; set; } public bool ThreeDoor { get; set; } public bool FiveDoor { get; set; } } 

Second, modify your PopulateTreeView method as follows:

public void PopulateTreeView(int parentId, TreeNode parentNode) { // ... foreach (DataRow dr in dtchildc.Rows) { // Populate model info from the data var modelInfo = new ModelInfo { Name = dr["ModelName"].ToString() }; //modelInfo.TwoDoor = dr[...]; //modelInfo.ThreeDoor = dr[...]; //modelInfo.FiveDoor = dr[...]; // Create and add a new node var childNode = (parentNode == null ? treeView1.Nodes : parentNode.Nodes).Add(modelInfo.Name); // Associate info with the node childNode.Tag = modelInfo; } // ... } 

The essential part is to store the ModelInfo inside the Node.Tag property so it can be retrieved later.

Finally, subscribe to the TreeView.AfterSelect event and put inside the event handler something like this:

// Get the model info from the selected node var modelInfo = e.Node != null ? e.Node.Tag as ModelInfo : null; // Update the checkboxes accordingly chkTwoDoor.Checked = modelInfo != null && modelInfo.TwoDoor; chkThreeDoor.Checked = modelInfo != null && modelInfo.ThreeDoor; chkFiveDoor.Checked = modelInfo != null && modelInfo.FiveDoor; 

for the two-state checkbox style, or alternatively

chkTwoDoor.CheckState = modelInfo == null ? CheckState.Indeterminate : modelInfo.TwoDoor ? CheckState.Checked : CheckState.Unchecked; chkThreeDoor.CheckState = modelInfo == null ? CheckState.Indeterminate : modelInfo.ThreeDoor ? CheckState.Checked : CheckState.Unchecked; chkFiveDoor.CheckState = modelInfo == null ? CheckState.Indeterminate : modelInfo.FiveDoor ? CheckState.Checked : CheckState.Unchecked; 

for the tri-state checkbox style.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, Ivan, when i click on a ``ParentNode` it throws a NullReferenceException?
Oops, sorry, the condition was wrong - I was thinking to include a code for tri state, then decided not to, and of course left the wrong condition. Of course it should be != null && ...
Thanks, could you edit one condition for tri state?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.