public DataTable GetDataTableHeirarchichal() { CostCentre[] tree = CostCentreDB.GetTree(); ArrayList list = Flatten(tree, "", "-1", 0); DataTable dt = new DataTable(); dt.Columns.Add("costcentre_id"); dt.Columns.Add("descr"); dt.Columns.Add("parent_id"); dt.Columns.Add("parent_descr"); dt.Columns.Add("level"); foreach (Hashtable hashTable in list) { DataRow newRow = dt.NewRow(); newRow["costcentre_id"] = hashTable["costcentre_id"].ToString(); newRow["descr"] = hashTable["descr"].ToString(); newRow["parent_id"] = hashTable["parent_id"].ToString(); newRow["parent_descr"] = hashTable["parent_descr"].ToString(); newRow["level"] = hashTable["level"].ToString(); dt.Rows.Add(newRow); } return(dt); }
protected void GrdCostCentre_RowUpdating(object sender, GridViewUpdateEventArgs e) { Label lblId = (Label)GrdCostCentre.Rows[e.RowIndex].FindControl("lblId"); DropDownList ddlParent = (DropDownList)GrdCostCentre.Rows[e.RowIndex].FindControl("ddlParent"); TextBox txtDescr = (TextBox)GrdCostCentre.Rows[e.RowIndex].FindControl("txtDescr"); // make sure we are not parent up the line of parent_id CostCentre[] tree = CostCentreDB.GetTree(); CostCentre thisNode = GetRoot(tree, Convert.ToInt32(lblId.Text)); if (ChildrenContains(thisNode, Convert.ToInt32(ddlParent.SelectedValue))) { lblErrorMessage.Visible = true; lblErrorMessage.Text = "Can not add this parent because this is already a child"; return; } CostCentreDB.Update(Convert.ToInt32(lblId.Text), txtDescr.Text, Convert.ToInt32(ddlParent.SelectedValue)); GrdCostCentre.EditIndex = -1; FillGrid(); }