New to Telerik UI for ASP.NET AJAXStart a free 30-day trial

Connections

In this article you can check how to data bind the connections in a RadDiagram control. The diagram can use the standard data sources for binding an ASP.NET control:

  • Declarative ASP.NET data sources (SqlDatasource, ObjectDataSource, LinqDataSource, etc.)

  • Server-side collections that implement the IEnumerable interface.

Before data binding the connections you need to have shapes in the diagram so that the controls knows the start and end of each connection. This is why the diagram in Example 1 has data binding of shapes as well.

Similar to setting a data source for an ASP.NET control, you should either use the ConnectionDataSourceID property when data binding declaratively or ConnectionDataSource when the data source of the control is set on the server-side (such it the case in Example 1).

The data source should have columns for each of the diagram fields that you want to specify. For example, if you want to pull the start cap type of the connection from the data source, this will require a separate column that contains the start cap type of each connection.

The columns of the data source are attached to their corresponding data fields in the connections via the properties with the structure Data[field name]Field in RadDiagram > BindingSettings > ConnectionSettings. You can find the full set of properties in the Server-Side API of RadDiagram.

Example 1: Data binding shapes and connections in a diagram to a collection of custom objects.

ASP.NET
<telerik:RadDiagram ID="RadDiagram1" runat="server">
	<BindingSettings>
		<ShapeSettings DataContentTextField="text" DataIdField="id" DataTypeField="type"
			DataXField="x" DataYField="y" />
		<ConnectionSettings DataFromShapeIdField="sourceShapeId" DataFromConnectorField="sourceConnector"
			DataToShapeIdField="targetShapeId" DataToConnectorField="targetConnector" DataStartCapField="startCapField" 
			DataEndCapField="endCapField"  />
	</BindingSettings>
</telerik:RadDiagram>
C#
protected void Page_Load(object sender, EventArgs e)
{
	if (!IsPostBack)
	{
		RadDiagram1.DataSource = GetShapes();
		RadDiagram1.ConnectionDataSource = GetConnections();
		RadDiagram1.DataBind();
	}
}

private IEnumerable GetShapes()
{
	return new[] {
		new {id="Shape1", text="Shape 1", type="circle", x=100, y=300, width=300, height=100},
		new {id="Shape2", text="Shape 2", type="rectangle", x=200, y=100, width=200, height=200},
		new {id="Shape3", text="Shape 3", type="circle", x=300, y=300, width=300, height=100},
		new {id="Shape4", text="Shape 4", type="rectangle", x=400, y=100, width=200, height=200},
		new {id="Shape5", text="Shape 5", type="circle", x=500, y=300, width=300, height=100}
	};
}

private IEnumerable GetConnections()
{
	return new[] {
		new {sourceShapeId="Shape2", sourceConnector="auto", targetShapeId="Shape1", targetConnector="auto", startCapField=Telerik.Web.UI.Diagram.ConnectionStartCap.FilledCircle, 
			endCapField=Telerik.Web.UI.Diagram.ConnectionEndCap.ArrowEnd},
		new {sourceShapeId="Shape2", sourceConnector="auto", targetShapeId="Shape3", targetConnector="auto", startCapField=Telerik.Web.UI.Diagram.ConnectionStartCap.FilledCircle, 
			endCapField=Telerik.Web.UI.Diagram.ConnectionEndCap.ArrowEnd},
		new {sourceShapeId="Shape4", sourceConnector="auto", targetShapeId="Shape5", targetConnector="auto", startCapField=Telerik.Web.UI.Diagram.ConnectionStartCap.FilledCircle, 
			endCapField=Telerik.Web.UI.Diagram.ConnectionEndCap.ArrowEnd}
	};
}
Not finding the help you need?
Contact Support