Generating Diagrams from a Data Source
- 3 minutes to read
The DiagramDataBindingController class provides the data binding functionality for the DiagramControl. To bind the DiagramControl to data, open its Smart Tag panel and select Add DataBindingController.
Note
Starting with v20.1, the DiagramDataBindingController supports DataTable and DataSet sources.
To configure the data source, go to the diagramDataBindingController‘s Smart Tag.
The following table lists the properties used to map a diagram to data.
Member | Description |
---|---|
DiagramDataBindingController.DataSource | Specifies the path to a collection of diagram items. |
Specifies the path to a collection of objects that represent connectors. | |
Specifies the name of the data field that identifies the item to use as a connector’s start item. | |
Specifies the name of the data field that identifies the item to use as a connector’s end item. |
Tip
The Item Template Designer allows you to design templates for diagram items and connectors at design time.
The following example demonstrates the DiagramControl bound to sample data.
public class ViewModel {
public List<Item> Items { get; set; }
public List<Link> Connections { get; set; }
public ViewModel() {
Items = new List<Item>();
for (int i = 0; i < 5; i++)
Items.Add(new Item { Id = i, Name = "Item " + i });
Connections = new List<Link>();
for (int i = 0; i < 4; i++)
Connections.Add(new Link { From = Items[i].Id, To = Items[i + 1].Id });
Connections.Add(new Link { From = Items[4].Id, To = Items[0].Id });
}
}
public class Item {
public int Id { get; set; }
public string Name { get; set; }
}
public class Link {
public object From { get; set; }
public object To { get; set; }
}
public Form1() {
InitializeComponent();
ViewModel viewModel = new ViewModel();
diagramDataBindingController1.GenerateItem += DiagramDataBindingController1_GenerateItem;
diagramDataBindingController1.DataSource = viewModel.Items;
diagramDataBindingController1.KeyMember = "Id";
diagramDataBindingController1.ConnectorsSource = viewModel.Connections;
diagramDataBindingController1.ConnectorFromMember = "From";
diagramDataBindingController1.ConnectorToMember = "To";
diagramDataBindingController1.LayoutKind = DiagramLayoutKind.Circular;
}
private void DiagramDataBindingController1_GenerateItem(object sender, DiagramGenerateItemEventArgs e) {
var item = new DiagramShape {
X = 27,
Width = 75,
Height = 50,
Shape = BasicShapes.Rectangle
};
item.Bindings.Add(new DiagramBinding("Content", "Name"));
e.Item = item;
}