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:
-
private void CreateTree()
-
{
-
treeView.BeginUpdate();
-
treeView.Nodes.Clear();
-
TreeNode tnSiteHeader = new TreeNode("Sites");
-
treeView.Nodes.Add(tnSiteHeader);
-
foreach (DataRow drSite in stamdataDataSet.Tables["Site"].Rows)
-
{
-
TreeNode tnSite= new TreeNode(drSite["Name"].ToString());
-
string[] tagSiteObj = new string[2];
-
tagSiteObj[0] = "";
-
tagSiteObj[1] = stamdataDataSet.Site.ToString();
-
tnSiteHeader.Tag = tagSiteObj;
-
tnSiteHeader.Nodes.Add(tnSite);
-
TreeNode tnLineHeader = new TreeNode("Lines");
-
tnSite.Nodes.Add(tnLineHeader);
-
-
foreach (DataRow drLine in drSite.GetChildRows("FK_ProductionEquiptment_Site"))
-
{
-
TreeNode tnLine = new TreeNode(drLine["Name"].ToString());
-
string[] tagLineObj = new string[2];
-
tagLineObj[0] = string.Format("SiteID = {0}", drLine["SiteID"].ToString());
-
tagLineObj[1] = stamdataDataSet.Line.ToString();
-
tnLineHeader.Tag = tagLineObj;
-
tnLineHeader.Nodes.Add(tnLine);
-
TreeNode tnLineControllerHeader = new TreeNode("LineControllers");
-
tnLine.Nodes.Add(tnLineControllerHeader);
-
-
foreach (DataRow drLineController in drLine.GetChildRows("FK_LineController_Line"))
-
{
-
TreeNode tnLineController = new TreeNode(drLineController["DBPath"].ToString());
-
string[] tagLineConObj = new string[2];
-
tagLineConObj[0] = string.Format("LineID = {0}", drLineController["LineID"].ToString());
-
tagLineConObj[1] = stamdataDataSet.LineController.ToString();
-
tnLineControllerHeader.Tag = tagLineConObj;
-
tnLineControllerHeader.Nodes.Add(tnLineController);
-
}
-
}
-
treeView.EndUpdate();
-
}
-
-
private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
-
{
-
dgvStamdata.DataSource = null;
-
if (this.treeView.SelectedNode.Tag != null)
-
{
-
string[] tagObj = (string[])e.Node.Tag;
-
string idfilter = (string)tagObj[0];
-
string datamember = (string)tagObj[1];
-
-
DataTable dt = stamdataDataSet.Tables[datamember];
-
dt.DefaultView.RowFilter = idfilter;
-
-
dgvStamdata.DataSource = dt;
-
}
-
}
-