原创|其它|编辑:郝浩|2012-07-12 21:52:55.000|阅读 422 次
概述:上述在设计时中的步骤可以通过一些编码来实现。这使您能更加自由和完全的控制数据库操作。每个数据序列都拥有“DataSource”属性,属性决定了数据库值(表或查询)的来源,并且“YValues.DataMember”和“LabelMember”属性指定我们想要绘制的字段。
# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>
[.NET 2003版本]
DataAdapter
上述在设计时中的步骤可以通过一些编码来实现。这使您能更加自由和完全的控制数据库操作。
每个数据序列都拥有“DataSource”属性,属性决定了数据库值(表或查询)的来源,并且“YValues.DataMember”和“LabelMember”属性指定我们想要绘制的字段。
将一个新的TeeChart for .NET项目拖拽一个新的项目和一个新的窗体上。
[C#]
using System.Data;
using System.Data.OleDb;
using System.Security;
using System.Security.Permissions;
private void Form1_Load(object sender, System.EventArgs e) {
DataSet masterDataSet = new DataSet();
Bar bar1 = new Bar(tChart1.Chart);
try
{
OpenConnWithJet40(ref masterDataSet);
DataTable employeeTable = masterDataSet.Tables["EMPLOYEE"];
bar1.YValues.DataMember = employeeTable.Columns["SALARY"].ToString();
bar1.LabelMember = employeeTable.Columns["LASTNAME"].ToString();
bar1.DataSource = employeeTable;
}
catch (SecurityException)
{
MessageBox.Show("The calling method has not been granted sufficient permission to access the Microsoft Jet 4.0 OLE DB Provider.");
}
}
[OleDbPermission(SecurityAction.Demand, Provider = "Microsoft.Jet.OLEDB.4.0")]
private void OpenConnWithJet40(ref DataSet masterDataSet) {
OleDbConnection testConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + @"Data Source=C:\Program Files\Steema Software\TeeChart for .NET v3\Sample data\TeeChart.mdb;");
testConn.Open();
string strCom = "SELECT * FROM Employee";
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom,testConn);
myCommand.Fill(masterDataSet, "Employee");
testConn.Close();
}
[VB.Net]
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim MasterDataSet As New DataSet()
Dim Bar1 As New Steema.TeeChart.Styles.Bar(TChart1.Chart)
Try
OpenConnWithJet40(MasterDataSet)
Dim employeeTable As DataTable = MasterDataSet.Tables("Employee")
Bar1.YValues.DataMember = employeeTable.Columns("SALARY").ToString()
Bar1.LabelMember = employeeTable.Columns("LASTNAME").ToString()
Bar1.DataSource = employeeTable
Catch ex As Exception
MessageBox.Show("Microsoft Jet 4.0 OLE DB Provider problem: " & ex.Message)
End Try
End Sub
Private Sub OpenConnWithJet40(ByRef MasterDataSet As DataSet)
Dim TestConn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=C:\Program Files\Steema Software\TeeChart for .NET v3\Sample data\TeeChart.mdb;")
TestConn.Open()
Dim StrCom As String = "SELECT * FROM Employee"
Dim MyCommand As New OleDbDataAdapter(StrCom, TestConn)
MyCommand.Fill(MasterDataSet, "Employee")
TestConn.Close()
End Sub
End Class
在.NET 2005版本中,稍微有一些针对2005版的变化。
[.NET 2005版本]
DataAdapter
[C#]
using System.Data;
using System.Data.OleDb;
using System.Security;
using System.Security.Permissions;
private void Form1_Load(object sender, EventArgs e)
{
//string ConnString = SqlDataSource1.ConnectionString;
string sQuery = "select * from Employee";
//SqlConnection myConnection = new SqlConnection(ConnString);
string myConnString = "Driver={Microsoft Access Driver (*.mdb)};Dbq=C:\\Program Files\\Steema Software\\TeeChart for .NET v3\\Sample data\\TeeChart.mdb;Uid=Admin;Pwd=;";
System.Data.Odbc.OdbcConnection myConnection = new System.Data.Odbc.OdbcConnection(myConnString);
//SqlCommand myCommand = new SqlCommand(sQuery, myConnection);
System.Data.Odbc.OdbcCommand myCommand = new System.Data.Odbc.OdbcCommand(sQuery, myConnection);
myConnection.Open();
//SqlDataReader myReader = myCommand.ExecuteReader();
System.Data.Odbc.OdbcDataReader myReader = myCommand.ExecuteReader();
int XColumn = myReader.GetOrdinal("ID");
int YColumn = myReader.GetOrdinal("SALARY");
int LabelColumn = myReader.GetOrdinal("LASTNAME");
Steema.TeeChart.Styles.Bar bar1 = new Steema.TeeChart.Styles.Bar(tChart1.Chart);
while (myReader.Read())
{
bar1.Add(Convert.ToInt32(myReader[XColumn]), Convert.ToDouble(myReader[YColumn]), Convert.ToString(myReader[LabelColumn]));
}
myReader.Close();
myConnection.Close();
}
[VB.Net]
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Loa
Dim MasterDataSet As New DataSet()
Dim Bar1 As New Steema.TeeChart.Styles.Bar(TChart1.Chart)
Try
OpenConnWithJet40(MasterDataSet)
Dim employeeTable As DataTable = MasterDataSet.Tables("EMPLOYEE")
Bar1.YValues.DataMember = employeeTable.Columns("SALARY").ToString()
Bar1.LabelMember = employeeTable.Columns("LASTNAME").ToString()
Bar1.DataSource = employeeTable
Catch ex As Exception
MessageBox.Show("Microsoft Jet 4.0 OLE DB Provider problem: " & ex.Message)
End Try
End Sub
Private Sub OpenConnWithJet40(ByRef MasterDataSet As DataSet)
Dim TestConn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=C:\Program Files\Steema Software\TeeChart for .NET v3\Sample data\TeeChart.mdb;")
TestConn.Open()
Dim StrCom As String = "SELECT * FROM Employee"
Dim MyCommand As New OleDb.OleDbDataAdapter(StrCom, TestConn)
MyCommand.Fill(MasterDataSet, "Employee")
TestConn.Close()
End Sub
End Class
如果数据库的值发生改变,你要“refresh”连接并重新检索值,你应该这样做:
TChart1.Series(0).CheckDataSource
“CheckDataSource”方法会迫使数据序列重新检索所有的记录。
所有的数据序列同样拥有“XValues.ValueSource”属性,这个属性只用在XY图表中(当你在每个点都拥有X坐标)。
单条记录
连接到表
使用图表编辑器将在窗体的初始化中创建下面的代码。类似的代码可用于在运行时创建并连接新的单条记录数据源。
(假设一个DataSet调用sourceTableSet的表单:sourceTable)
this.singleRecordSource1 = new Steema.TeeChart.Data.SingleRecordSource();
this.sourceTableSet.DataSetName = "NewDataSet";
this.sourceTableSet.Tables.AddRange(new System.Data.DataTable[] {this.sourceTable});
this.singleRecordSource1.DataSource = this.sourceTableSet;
this.singleRecordSource1.ValueMembers = new string[] {
"JAN",
"FEB",
"MAR"};
this.bar1 = new Steema.TeeChart.Styles.Bar();
this.bar1.DataSource = this.singleRecordSource1;
表单检索
TeeChart的SingleRecord使用CurrencyManager组件来检索数据库的表单并更新到图表。在表单中的记录变更后应该使用CheckDatasource来更新图表。
例子
private CurrencyManager myCurrencyManager;
public void ConnectChartToTable()
{
myCurrencyManager = (CurrencyManager)this.BindingContext[sourceTable];
singleRecordSource1.RecordCurrency=myCurrencyManager;
tChart1[0].CheckDataSource();
}
表单(和图表)可以通过CurrencyManager来进行检索。
private void MovePrevious()
{
if (myCurrencyManager.Position>0)
{
myCurrencyManager.Position=myCurrencyManager.Position+1;
tChart1[0].CheckDataSource();
}
}
private void MoveNext()
{
if (myCurrencyManager.Position<myCurrencyManager.Count)
{
myCurrencyManager.Position=myCurrencyManager.Position+1;
tChart1[0].CheckDataSource();
}
}
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@pclwef.cn
文章转载自:慧都控件网