本主题介绍各种数据绑定技术。有关将数据绑定到序列的教程,请参见教程:将图表与数据库进行数据绑定。
数据绑定方法
方法  | 
优点  | 
缺点  | 
|---|---|---|
Chart.DataBindTable  | 
  | 
  | 
Chart.DataSource 和 Chart.DataBind  | 
  | 
  | 
Points.DataBind(X)Y  | 
  | 
  | 
Points.DataBind  | 
同上,另外还有: 
  | 
  | 
Chart.DataBindCrossTable  | 
  | 
  | 
数据源
下面是可以用于绑定的数据源:
DataView。
数据读取器(SQL、OleDB)。
DataSet(仅限于 DataSource 数据绑定)。
数组。
列表。
所有 IEnumerable 对象。
 注意 | 
|---|
在使用列表或数组等非表格数据源时,只能绑定 Y 值,无论所使用的数据绑定方法是何类型。这是因为不能为 X 值和其他图表属性(如 Tooltip)指定列。  | 
示例
下面的代码演示如何将一个柱形图绑定到一个 Access 数据库表。表“SALESCOUNTS”具有一个含有销售人员姓名的“SalesRep”列、一个“Prod A”列、一个“Prod B”列、一个“Prod C”列、一个“Prod D”列、一个“Prod E”列和一个“Other”列。DataBindTable 方法自动创建六个 Series 对象:每个产品列各一个,另一个用于“Other”列。对于每个记录,每个序列中自动存在一个数据点。该方法还将六个序列的 X 值绑定到“SalesRep”列,并将其用于数据点的轴标签。
Imports System.Data.OleDb 
Imports System.Data 
Imports System.Web.UI.DataVisualization.Charting
...
' Resolve the address to the Access database. We assume database is 
' in Bin folder. 
Dim fileNameString As String =  "chartdata.mdb" 
' Initialize a connection string. 
Dim myConnectionString As String =  "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileNameString 
' Define the database query. 
Dim mySelectQuery As String = "SELECT * FROM SALESCOUNTS;" 
' Create a database connection object using the connection string. 
Dim myConnection As OleDbConnection =  New OleDbConnection(myConnectionString) 
' Create a database command on the connection using query. 
Dim myCommand As OleDbCommand =  New OleDbCommand(mySelectQuery,myConnection) 
' Open the connection. 
myCommand.Connection.Open() 
' Create a database reader. 
Dim myReader As OleDbDataReader =  myCommand.ExecuteReader(CommandBehavior.CloseConnection) 
' Specify the Name column to be used for point's X values. 
chart1.DataBindTable(myReader,"SalesRep")
' Close the connection. 
myConnection.Close()
'  This is a loop to set all created charts appearance with custom attribute.
Dim series As Series
For Each series In chart1.Series
    series.CustomAttributes = "DrawingStyle=LightToDark"
Next
using System.Data.OleDb; 
using System.Data; 
using System.Web.UI.DataVisualization.Charting;
...
// Resolve the address to the Access database. We assume database is 
// in Bin folder. 
string fileNameString = "chartdata.mdb"; 
// Initialize a connection string. 
string myConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileNameString; 
// Define the database query. 
string mySelectQuery="SELECT * FROM SALESCOUNTS;"; 
// Create a database connection object using the connection string. 
OleDbConnection myConnection = new OleDbConnection(myConnectionString); 
// Create a database command on the connection using query. 
OleDbCommand myCommand = new OleDbCommand(mySelectQuery, myConnection); 
// Open the connection. 
myCommand.Connection.Open(); 
// Create a database reader. 
OleDbDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection); 
// Specify the Name column to be used for point's X values. 
chart1.DataBindTable(myReader,"SalesRep");
// Close the connection. 
myConnection.Close();
//  This is a loop to set all created charts appearance with custom attribute.
foreach (Series series in chart1.Series)
{
    series.CustomAttributes = "DrawingStyle=LightToDark";
}
请参阅
参考
System.Windows.Forms.DataVisualization.Charting
System.Web.UI.DataVisualization.Charting
注意