Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
739
need example of binding to a DataSet
posted

I have a DataSet that already has a hierarchical relationship between the two DataTables it contains and I want to bind this to a two-level WebDataTree.  I can't find any examples of how to do this.  Just setting the DataSource to the DataSet doesn't seem to work.  Our architecture involves a DAL layer and a business layer that returns either custom object collections, or sometimes DataTables/DataSets.

Can someone help me with this?  I want to use this new control but this has me stumped.

Ideally for each of the two levels I can set the visible text of the node to a description field from the corresponding DataTable and the value to an id field so that I can refresh a WebDataGrid based on the clicked node.

Thanks.

Parents
No Data
Reply
  • 3147
    Verified Answer
    posted

    Hi,

    Here is a simple example of WebDataTree bound to a DataSet with load-on-demand enabled:

     <ig:WebDataTree ID="WebDataTree1" runat="server" Height="500px" Width="400px"
      InitialExpandDepth="0" InitialDataBindDepth="0" OnNodePopulate="WebDataTree1_NodePopulate">
      <DataBindings>
       <ig:DataTreeNodeBinding DataMember="Test" TextField="Text" ValueField="Id" />
       <ig:DataTreeNodeBinding DataMember="Emploeyees" TextField="LastName" ValueField="EmployeeID" />
       <ig:DataTreeNodeBinding DataMember="EmployeeTerritories" TextField="TerritoryID" ValueField="TerritoryID" />
       <ig:DataTreeNodeBinding DataMember="Orders" TextField="OrderID" ValueField="OrderID" />
      </DataBindings>
     </ig:WebDataTree>

    Code behind:

        protected void Page_Load(object sender, EventArgs e)
        {
      if (!IsPostBack)
      {
       DataSet data = GetData();
       WebDataTree1.DataSource = data;
       WebDataTree1.DataBind();
      }
        }

     protected void WebDataTree1_NodePopulate(object sender, DataTreeNodeEventArgs e)
     {
      WebDataTree1.DataSource = GetData();
     }

     private DataSet GetData()
     {
      string connString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;

      string selectEmploeyeesSql = "SELECT * FROM Employees";
      string selectEmployeeTerritoriesSql = "SELECT * FROM EmployeeTerritories";

      DataSet data = new DataSet();

      DataTable tableTest = new DataTable("Test");
      tableTest.Columns.Add("Id", typeof(int));
      tableTest.Columns.Add("Text", typeof(string));

      tableTest.Rows.Add(1, "test 1");
      tableTest.Rows.Add(2, "test 2");

      SqlDataAdapter adapter = new SqlDataAdapter(selectEmploeyeesSql, connString);
      DataTable tableEmploeyees = new DataTable("Emploeyees");
      adapter.Fill(tableEmploeyees);

      tableEmploeyees.PrimaryKey = new DataColumn[] { tableEmploeyees.Columns["EmployeeId"] };

      adapter = new SqlDataAdapter(selectEmployeeTerritoriesSql, connString);
      DataTable tableEmployeeTerritories = new DataTable("EmployeeTerritories");
      adapter.Fill(tableEmployeeTerritories);

      data.Tables.Add(tableTest);
      data.Tables.Add(tableEmploeyees);
      data.Tables.Add(tableEmployeeTerritories);

      DataColumn parentColumn = tableEmploeyees.Columns["EmployeeId"];
      DataColumn childColumn = tableEmployeeTerritories.Columns["EmployeeId"];

      data.Relations.Add(parentColumn, childColumn);

      return data;
     }

Children