Connecting Tech Pros Worldwide Help | Site Map

C#-FORM: Populate treeview from dataset and edit the tables in datagridview

Newbie
 
Join Date: Dec 2007
Posts: 1
#1: Dec 10 '07
Hello

I have populated a treeview from my dataset and by clicking in the treeview i want to get the corresponding table shown in a datagridview. Ive managed this by looking at each DataRow and child rows and added the nodes with a columnname from each datarow. At the same time i have put in a tag at each treenode with a filter string and the current table im looking at as a string to use for later so i can create a datatable and set a filter to that and set it as datasource to the datagridview.
So to fill my datagridview with the corresponding data from where in the treeview ive clicked, i have made an AfterSelect event. In this event i get the tag from the clicked node and use the filter string to filter which rows i would like to be shown in the datagridview. I do this because the overall table is a "Site" beneath that ive got some "Lines" and so on. So when clicking "Lines" i would like to only get the "lines" beneath the specific "Site" shown. This all works fine.
But when i create a new "Site" i can see the child tables in the treeview which is empty ofcourse. But when clicking one of the child tables like "Lines" i would like to be able to create a new "Line" in my datagridview. But when clicking the node i cant do that as theres nothing shown in the datagridview, not even column headers.
I know that this is happening because theres no datarows in the table and so i never get my filter string and datasource set.
So i would like to hear any ideas on how to get my empty tables shown in the datagridview so i can fill in new data or maybe a completly different solution to this.


Code:
Expand|Select|Wrap|Line Numbers
  1. private void CreateTree()
  2. {
  3. treeView.BeginUpdate();
  4. treeView.Nodes.Clear();
  5. TreeNode tnSiteHeader = new TreeNode("Sites");
  6. treeView.Nodes.Add(tnSiteHeader);
  7. foreach (DataRow drSite in stamdataDataSet.Tables["Site"].Rows)
  8. {
  9. TreeNode tnSite= new TreeNode(drSite["Name"].ToString());
  10. string[] tagSiteObj = new string[2];
  11. tagSiteObj[0] = "";
  12. tagSiteObj[1] = stamdataDataSet.Site.ToString();
  13. tnSiteHeader.Tag = tagSiteObj;
  14. tnSiteHeader.Nodes.Add(tnSite);
  15. TreeNode tnLineHeader = new TreeNode("Lines");
  16. tnSite.Nodes.Add(tnLineHeader);
  17.  
  18. foreach (DataRow drLine in drSite.GetChildRows("FK_ProductionEquiptment_Site"))
  19. {
  20. TreeNode tnLine = new TreeNode(drLine["Name"].ToString());
  21. string[] tagLineObj = new string[2];
  22. tagLineObj[0] = string.Format("SiteID = {0}", drLine["SiteID"].ToString());
  23. tagLineObj[1] = stamdataDataSet.Line.ToString();
  24. tnLineHeader.Tag = tagLineObj;
  25. tnLineHeader.Nodes.Add(tnLine);
  26. TreeNode tnLineControllerHeader = new TreeNode("LineControllers");
  27. tnLine.Nodes.Add(tnLineControllerHeader);
  28.  
  29. foreach (DataRow drLineController in drLine.GetChildRows("FK_LineController_Line"))
  30. {
  31. TreeNode tnLineController = new TreeNode(drLineController["DBPath"].ToString());
  32. string[] tagLineConObj = new string[2];
  33. tagLineConObj[0] = string.Format("LineID = {0}", drLineController["LineID"].ToString());
  34. tagLineConObj[1] = stamdataDataSet.LineController.ToString();
  35. tnLineControllerHeader.Tag = tagLineConObj;
  36. tnLineControllerHeader.Nodes.Add(tnLineController);
  37. }
  38. }
  39. treeView.EndUpdate();
  40. }
  41.  
  42. private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
  43. {
  44. dgvStamdata.DataSource = null;
  45. if (this.treeView.SelectedNode.Tag != null)
  46. {
  47. string[] tagObj = (string[])e.Node.Tag;
  48. string idfilter = (string)tagObj[0];
  49. string datamember = (string)tagObj[1];
  50.  
  51. DataTable dt = stamdataDataSet.Tables[datamember];
  52. dt.DefaultView.RowFilter = idfilter;
  53.  
  54. dgvStamdata.DataSource = dt;
  55. }
  56. }
  57.  
Reply