ASPX Code
<%@ Page Language="C#" MasterPageFile="~/SampleCode/SamplesMasterPage.master"AutoEventWireup="true" CodeFile="HittingADatabase.aspx.cs" Inherits="SampleCode_DatabaseExample_HittingADatabase" %>
<%@ Register TagPrefix="swc" Namespace="SmartWebControls" Assembly="SmartWebControls.SmartChart" %>
<asp:Content ID="content" runat="server" ContentPlaceHolderID="cphMain">
<div id="divTitle">
Binding Data from a Database</div>
<div id="divInstructions">
SmartChart allows hierarchical data to be bound to it for display in a Webpage. This example
demonstrates how data from a database can be converted into hierarchical data and then bound to the SmartChart
control. The database fields consist of PersonID, ParentID, Name and Title. Once the data is loaded into a DataSet
it is converted into a hierarchical view by using the DataRelation class. Click "View Code" above to examine the code.
</div>
<p />
<div>
<swc:SmartChartPro ID="SmartChart1" Title="Org Chart" runat="server" OutputType="Html" DataTitleFields="Name,Title"
Font-Size="8" DrawBorder="False" MaxTextLength="30" Width="872px" Height="250px"
DataNodeName="Employee" DataKeyField="PersonID" DataFields="Name,Title" BoxColor="Gainsboro"
BoxGradient="True" ChartDepth="5" Font-Bold="false" MaxChildrenPerLevelGroup="6"
HasParentImagePath="../Images/up.gif" HasChildrenImagePath="../Images/down.gif"
DrillDownType="SmartChartImage">
</swc:SmartChartPro>
</div>
</asp:Content>
C# Code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class SampleCode_DatabaseExample_HittingADatabase : System.Web.UI.Page
{
protected void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
SmartChart1.DataSource = GetDataSource();
SmartChart1.DataBind();
}
}
private DataSet GetDataSource()
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SmartWebControlsConnectionString"].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM People", conn);
DataSet ds = new DataSet();
da.Fill(ds, "Employee");
//Create parent/child relationships based upon ID and parentID fields
DataColumn colParent = ds.Tables[0].Columns["PersonID"];
DataColumn colChild = ds.Tables[0].Columns["ParentID"];
DataRelation relation = new DataRelation("ParentChildRel", colParent, colChild);
relation.Nested = true;
ds.Relations.Add(relation);
return ds;
}
private void SmartChart1_SmartChartClicking(object sender, SmartWebControls.SmartChartEventArgs e)
{
Response.Write("The item you clicked has an ID of: " + e.Item.DataKey);
}
}